1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkArcParallelEdgeStrategy.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
/*----------------------------------------------------------------------------
Copyright (c) Sandia Corporation
See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
----------------------------------------------------------------------------*/
#include "vtkArcParallelEdgeStrategy.h"
#include "vtkCellArray.h"
#include "vtkCommand.h"
#include "vtkDirectedGraph.h"
#include "vtkEdgeListIterator.h"
#include "vtkGraph.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPoints.h"
#include "vtkSmartPointer.h"
#include <vtksys/stl/utility>
#include <vtksys/stl/vector>
#include <vtksys/stl/map>
vtkStandardNewMacro(vtkArcParallelEdgeStrategy);
vtkArcParallelEdgeStrategy::vtkArcParallelEdgeStrategy()
{
this->NumberOfSubdivisions = 10;
}
vtkArcParallelEdgeStrategy::~vtkArcParallelEdgeStrategy()
{
}
void vtkArcParallelEdgeStrategy::Layout()
{
bool directed = vtkDirectedGraph::SafeDownCast(this->Graph) != 0;
vtksys_stl::map<vtksys_stl::pair<vtkIdType, vtkIdType>, int> edgeCount;
vtksys_stl::map<vtksys_stl::pair<vtkIdType, vtkIdType>, int> edgeNumber;
vtksys_stl::vector<vtkEdgeType> edgeVector(this->Graph->GetNumberOfEdges());
vtkSmartPointer<vtkEdgeListIterator> it =
vtkSmartPointer<vtkEdgeListIterator>::New();
this->Graph->GetEdges(it);
double avgEdgeLength = 0.0;
while (it->HasNext())
{
vtkEdgeType e = it->Next();
vtkIdType src, tgt;
if (directed || e.Source < e.Target)
{
src = e.Source;
tgt = e.Target;
}
else
{
src = e.Target;
tgt = e.Source;
}
edgeCount[vtksys_stl::pair<vtkIdType, vtkIdType>(src, tgt)]++;
edgeVector[e.Id] = e;
// Compute edge length
double sourcePt[3];
double targetPt[3];
this->Graph->GetPoint(e.Source, sourcePt);
this->Graph->GetPoint(e.Target, targetPt);
avgEdgeLength +=
sqrt(vtkMath::Distance2BetweenPoints(sourcePt, targetPt));
}
vtkIdType numEdges = this->Graph->GetNumberOfEdges();
if (numEdges > 0)
{
avgEdgeLength /= numEdges;
}
else
{
avgEdgeLength = 1.0;
}
double maxLoopHeight = avgEdgeLength / 10.0;
double* pts = new double[this->NumberOfSubdivisions*3];
for (vtkIdType eid = 0; eid < numEdges; ++eid)
{
vtkEdgeType e = edgeVector[eid];
vtkIdType src, tgt;
if (directed || e.Source < e.Target)
{
src = e.Source;
tgt = e.Target;
}
else
{
src = e.Target;
tgt = e.Source;
}
// Lookup the total number of edges with this source
// and target, as well as how many times this pair
// has been found so far.
vtksys_stl::pair<vtkIdType,vtkIdType> p(src, tgt);
edgeNumber[p]++;
int cur = edgeNumber[p];
int total = edgeCount[p];
vtksys_stl::pair<vtkIdType,vtkIdType> revP(tgt, src);
int revTotal = edgeCount[revP];
double sourcePt[3];
double targetPt[3];
this->Graph->GetPoint(e.Source, sourcePt);
this->Graph->GetPoint(e.Target, targetPt);
// If only one edge between source and target,
// just draw a straight line.
if (total + revTotal == 1)
{
double pt[6];
pt[0] = sourcePt[0];
pt[1] = sourcePt[1];
pt[2] = sourcePt[2];
pt[3] = targetPt[0];
pt[4] = targetPt[1];
pt[5] = targetPt[2];
this->Graph->SetEdgePoints(e.Id, 2, pt);
continue;
}
// Find vector from source to target
double delta[3];
for (int c = 0; c < 3; ++c)
{
delta[c] = targetPt[c] - sourcePt[c];
}
double dist = vtkMath::Norm(delta);
// If the distance is zero, draw a loop.
if (dist == 0)
{
double radius = maxLoopHeight*cur/total;
double u[3] = {1.0, 0.0, 0.0};
double v[3] = {0.0, 1.0, 0.0};
double center[3] = {sourcePt[0] - radius, sourcePt[1], sourcePt[2]};
// Use the general equation for a circle in three dimensions
// to draw a loop.
for (int s = 0; s < this->NumberOfSubdivisions; ++s)
{
double angle = 2.0*vtkMath::Pi()
*s/(this->NumberOfSubdivisions-1);
for (int c = 0; c < 3; ++c)
{
pts[3*s + c] = center[c]
+ radius*cos(angle)*u[c]
+ radius/2.0*sin(angle)*v[c];
}
}
this->Graph->SetEdgePoints(e.Id, this->NumberOfSubdivisions, pts);
continue;
}
// Find vector perpendicular to delta
// and (0,0,1).
double z[3] = {0.0, 0.0, 1.0};
double w[3];
vtkMath::Cross(delta, z, w);
vtkMath::Normalize(w);
// Really bad ascii art:
// ___-------___
// / |height\ <-- the drawn arc
// src----dist-----tgt
// \ | /
// \ |offset
// \ | /
// u \ | / x
// \ | /
// \ | /
// \|/
// center
// The center of the circle used to draw the arc is a
// point along the vector w a certain distance (offset)
// from the midpoint of sourcePt and targetPt.
// The offset is computed to give a certain arc height
// based on cur and total.
double maxHeight = dist/8.0;
double height;
int sign = 1;
if (directed)
{
// Directed edges will go on one side or the other
// automatically based on the order of source and target.
height = (static_cast<double>(cur)/total)*maxHeight;
}
else
{
// For undirected edges, place every other edge on one
// side or the other.
height = (static_cast<double>((cur+1)/2)/(total/2))*maxHeight;
if (cur % 2)
{
sign = -1;
}
}
// This formula computes offset given dist and height.
// You can pull out your trig formulas and verify it :)
double offset = (dist*dist/4.0 - height*height)/(2.0*height);
double center[3];
for (int c = 0; c < 3; ++c)
{
center[c] = (targetPt[c] + sourcePt[c])/2.0 + sign*offset*w[c];
}
// The vectors u and x are unit vectors pointing from the
// center of the circle to the two endpoints of the arc,
// sourcePt and targetPt, respectively.
double u[3], x[3];
for (int c = 0; c < 3; ++c)
{
u[c] = sourcePt[c] - center[c];
x[c] = targetPt[c] - center[c];
}
double radius = vtkMath::Norm(u);
vtkMath::Normalize(u);
vtkMath::Normalize(x);
// Find the angle that the arc spans.
double theta = acos(vtkMath::Dot(u, x));
// We need two perpendicular vectors on the plane of the circle
// in order to draw the circle. First we calculate n, a vector
// normal to the circle, by crossing u and w. Next, we cross
// n and u in order to get a vector v in the plane of the circle
// that is perpendicular to u.
double n[3];
vtkMath::Cross(u, w, n);
vtkMath::Normalize(n);
double v[3];
vtkMath::Cross(n, u, v);
vtkMath::Normalize(v);
// Use the general equation for a circle in three dimensions
// to draw an arc from the last point to the current point.
for (int s = 0; s < this->NumberOfSubdivisions; ++s)
{
double angle = -sign*s*theta/(this->NumberOfSubdivisions - 1.0);
for (int c = 0; c < 3; ++c)
{
pts[3*s + c] = center[c]
+ radius*cos(angle)*u[c]
+ radius*sin(angle)*v[c];
}
}
this->Graph->SetEdgePoints(e.Id, this->NumberOfSubdivisions, pts);
if (eid % 1000 == 0)
{
double progress = eid / static_cast<double>(numEdges);
this->InvokeEvent(vtkCommand::ProgressEvent, static_cast<void*>(&progress));
}
}
double progress = 1.0;
this->InvokeEvent(vtkCommand::ProgressEvent, static_cast<void*>(&progress));
delete [] pts;
}
void vtkArcParallelEdgeStrategy::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "NumberOfSubdivisions: " << this->NumberOfSubdivisions << endl;
}
|