Debian Codegraph @

07p/plaut04/src/tube.c++

raw
1 #include "tube.h"
2 #include "polygon3d.h"
3
4
5 #define COPY3(to,from) \
6 to[0] = from[0]; to[1] = from[1]; to[2] = from[2];
7
8
9 Tube::Tube(const int num_pts, float xyz_path[][3],
10 const float r, const int num_edges)
11 {
12 path = xyz_path;
13 // This is for checking the data validity.
14 // if distance betwee two points are too colse, the next
15 // point will be skipped.
16 // Added by ZCH. Mar 30, 2004
17 float distance = 0;
18 float pt[3];
19 pt[0]=path[0][0];
20 pt[1]=path[0][1];
21 pt[2]=path[0][2];
22 // for(int i =1; i <5; i++)
23 int i = 1;
24 int j = i;
25 int skip = 0;
26 do
27 {
28 distance = sqrt((path[i][0]-pt[0])*(path[i][0]-pt[0]) +
29 (path[i][1]-pt[1])*(path[i][1]-pt[1]) +
30 (path[i][2]-pt[2])*(path[i][2]-pt[2]));
31 if(distance < 1.e-7)
32 {
33 // printf("Skip i=%i\n", i);
34 skip++;
35 }
36 else
37 {
38 path[j][0]=path[i][0];
39 path[j][1]=path[i][1];
40 path[j][2]=path[i][2];
41
42 pt[0]=path[i][0];
43 pt[1]=path[i][1];
44 pt[2]=path[i][2];
45 j++;
46 }
47 i++;
48 } while(i < num_pts );
49
50 path_pts = i-skip;//num_pts;
51 // path_pts = num_pts;
52 // ZCH's modification end here.
53 radius = r;
54 n_edges = num_edges;
55
56 n_segments = path_pts - 1;
57 mesh_pts = (n_edges + 1) * path_pts;
58 open_path = (SbVec3f(path[0]) == SbVec3f(path[n_segments])) == 0;
59
60 // printf("Estimated number of mesh points: %d\n", mesh_pts);
61 }
62
63
64 SoSeparator* Tube::createTube()
65 {
66 SoSeparator *result = new SoSeparator;
67 result->ref();
68 // this line is added to deal with one or zero point cases.
69 // added by ZCH. Mar 30, 2004
70 if(path_pts <=1) return result;
71
72 // allocate memory for the array that stores vertices of the tube
73 mesh = new float[mesh_pts][3];
74
75 // compute the coordinates of the vertices
76 createMesh();
77
78 SoShapeHints *shapehints = new SoShapeHints;
79 shapehints->vertexOrdering = SoShapeHints::COUNTERCLOCKWISE;
80 shapehints->shapeType = SoShapeHints::SOLID;
81 shapehints->faceType = SoShapeHints::CONVEX;
82 shapehints->creaseAngle = 1.0;
83 result->addChild(shapehints);
84
85 // define coordinates for vertices
86 SoCoordinate3 *myCoords = new SoCoordinate3;
87 myCoords->point.setValues(0, mesh_pts, mesh);
88 result->addChild(myCoords);
89
90 // define the QuadMesh
91 SoQuadMesh *myQuadMesh = new SoQuadMesh;
92 myQuadMesh->verticesPerRow = n_edges + 1; // # of points around the tube
93 myQuadMesh->verticesPerColumn = path_pts; // # of points along the tube
94 result->addChild(myQuadMesh);
95
96 // if path is not closed, plug a solid polygon at each end of the tube
97 if ( open_path ) {
98
99 // for the start of the path, use the first n_edges points of mesh[]
100 // to define a polygon
101
102 // for the end of the path, use the last n_edges points from mesh[]
103 // with order reversed to get the correct orientation of the polygon
104
105 for (int i=0; i<n_edges; i++) {
106 COPY3(mesh[n_edges+i], mesh[mesh_pts-i-1]);
107 }
108
109 // set them as coordinate points
110 SoCoordinate3 *myCoords1 = new SoCoordinate3;
111 myCoords1->point.setValues(0, n_edges*2, mesh);
112 result->addChild(myCoords1);
113
114 // define the two polygons
115 SoFaceSet *myFaceSet = new SoFaceSet;
116 myFaceSet->startIndex.setValue(0);
117 myFaceSet->numVertices.set1Value(0, n_edges);
118 myFaceSet->numVertices.set1Value(1, n_edges);
119 result->addChild(myFaceSet);
120 }
121
122 // dealocate memory: don't need the array with vertices any more
123 delete[] mesh;
124
125 result->unrefNoDelete();
126 return result;
127 }
128
129
130 void Tube::createMesh()
131 {
132 int i, j;
133 int mp = 0; // counter for created mesh points
134 SbPlane plane;
135 float (*pj)[3] = new float[n_edges][3];
136 SbVec3f v1, v2;
137
138 // compute points of the first cross-section:
139
140 // place the polygon at the first point of the path
141 // and orient it perpendicular to the first segment
142 Polygon3D poly = Polygon3D(SbVec3f(path[0]),
143 SbVec3f(path[0]) - SbVec3f(path[1]), radius, n_edges);
144
145 if ( open_path ) {
146 // if the path is open, simply store the polygon vertices as mesh points
147 for (i=0; i<n_edges; i++) {
148 poly[i].getValue(mesh[mp][0], mesh[mp][1], mesh[mp][2]);
149 mp++;
150 }
151 poly[0].getValue(mesh[mp][0], mesh[mp][1], mesh[mp][2]);
152 mp++;
153 }
154 else {
155 // if the path is closed:
156
157 // define a plane through the first point that bisects the angle between
158 // the last and the first segments
159 v1.setValue(path[n_segments-1][0]-path[0][0],
160 path[n_segments-1][1]-path[0][1],
161 path[n_segments-1][2]-path[0][2]);
162 v1.normalize();
163 v2.setValue(path[0][0]-path[1][0],
164 path[0][1]-path[1][1],
165 path[0][2]-path[1][2]);
166 v2.normalize();
167 plane = SbPlane(v2+v1, SbVec3f(path[0]));
168
169 // project the vertices of the polygon onto the plane
170 poly.project(plane, pj);
171
172 // store the result as mesh points defining the first cross-section
173 for (i=0; i<n_edges; i++) {
174 COPY3(mesh[mp], pj[i]);
175 mp++;
176 }
177 COPY3(mesh[n_edges], pj[0]);
178 mp++;
179 }
180
181 // compute points of the intermediate cross-sections
182 for (j=0; j<n_segments-1; j++)
183 {
184 // move the polygon to the j+1 point of the path ...
185 poly.moveTo(SbVec3f(path[j+1]));
186
187 // and orient it perpendiclar to (j+1,j+2) segment
188 poly.rotate(SbVec3f(path[j+1]) - SbVec3f(path[j+2]));
189
190 // define a plane through j+1 point that bisects the angle between
191 // (j,j+1) and (j+1,j+2) segments
192 v1.setValue(path[j][0]-path[j+1][0],
193 path[j][1]-path[j+1][1],
194 path[j][2]-path[j+1][2]);
195 v1.normalize();
196 v2.setValue(path[j+1][0]-path[j+2][0],
197 path[j+1][1]-path[j+2][1],
198 path[j+1][2]-path[j+2][2]);
199 v2.normalize();
200
201 //////////////////////////////////////////////////////////////////////////////
202 // in case v1 and v2 are equal but in opposite direction?
203 // added by zch
204 /*
205 SbVec3f v1cv2 = v1.cross(v2);
206 //if(v1cv2.length()==0) // in case two are exactly opposite this work just fine, but..
207 double vdv = v1.dot(v2);
208 double theta = acos(v1.dot(v2));
209 printf("vdv = %f\n", vdv);
210 if(fabs(vdv+1.0) < 1.e-5)
211 {
212 // only do this when vdv ~= -1
213 printf("here \n");
214 plane = SbPlane(v1, SbVec3f(path[j+1]));
215 }
216 else
217 plane = SbPlane(v2+v1, SbVec3f(path[j+1]));
218 */
219 // BUT the above method still does not work in some case.
220 // Actually the simplest way to solve this is:
221 // NOTE: v1 doesn't work either.
222 plane = SbPlane(v2, SbVec3f(path[j+1]));
223
224 //////////////////////////////////////////////////////////////////////////////
225 /*
226 // Alternative code that only approximates bisection:
227 v1.setValue(path[j]); v2.setValue(path[j+2]);
228 plane = SbPlane(v1-v2, SbVec3f(path[j+1]));
229 */
230
231 // project (along the (j+1,j+2) segment) the vertices of the polygon
232 // onto the plane ...
233 poly.project(plane, pj);
234
235 // and store the result as mesh points
236 for (i=0; i<n_edges; i++)
237 {
238 COPY3(mesh[mp], pj[i]);
239 mp++;
240 }
241 COPY3(mesh[mp], pj[0]);
242 mp++;
243 }
244
245 // compute points of the last cross-section
246
247 // move the polygon to the last point of the path ...
248 poly.moveTo(SbVec3f(path[n_segments]));
249 // and orient it perpendicular to the last segment
250 poly.rotate(SbVec3f(path[n_segments-1]) - SbVec3f(path[n_segments]));
251
252 if ( open_path ) {
253 // if the path is open, simply store the polygon vertices as mesh points
254 for (i=0; i<n_edges; i++) {
255 poly[i].getValue(mesh[mp][0], mesh[mp][1], mesh[mp][2]);
256 mp++;
257 }
258 poly[0].getValue(mesh[mp][0], mesh[mp][1], mesh[mp][2]);
259 mp++;
260 }
261 else {
262 // if the path is closed:
263
264 // define a plane through the last point that bisects the angle between
265 // the last and the first segments
266 v1.setValue(path[n_segments-1][0]-path[0][0],
267 path[n_segments-1][1]-path[0][1],
268 path[n_segments-1][2]-path[0][2]);
269 v1.normalize();
270 v2.setValue(path[0][0]-path[1][0],
271 path[0][1]-path[1][1],
272 path[0][2]-path[1][2]);
273 v2.normalize();
274 plane = SbPlane(v2+v1, SbVec3f(path[0]));
275
276 // project the vertices of the polygon onto the plane
277 poly.project(plane, pj);
278
279 // store the result as mesh points defining the last cross-section
280 for (i=0; i<n_edges; i++) {
281 COPY3(mesh[mp], pj[i]);
282 mp++;
283 }
284 COPY3(mesh[mp], pj[0]);
285 mp++;
286 /*
287 for (i=0; i<n_edges; i++) {
288 COPY3(mesh[mp], mesh[i]);
289 mp++;
290 }
291 COPY3(mesh[mp], mesh[0]);
292 mp++;
293 */
294 }
295
296 // printf("Mesh points generated: %d\n", mp);
297 mesh_pts = mp;
298 delete[] pj;
299 }
300
301
302 #undef COPY3
303