Changeset 3362 for trunk/packages/vizservers/nanovis/ConvexPolygon.cpp
- Timestamp:
- Feb 26, 2013, 1:34:35 PM (12 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
-
Property
svn:mergeinfo
set to
/branches/nanovis2 merged eligible
-
Property
svn:mergeinfo
set to
-
trunk/packages/vizservers/nanovis/ConvexPolygon.cpp
r3177 r3362 26 26 } 27 27 28 // Finds the intersection of the line through29 // p1 and p2 with the given plane, putting the30 // result in intersect.31 //32 // If the line lies in the plane, an arbitrary33 // point on the line is returned.34 //35 // http://astronomy.swin.edu.au/pbourke/geometry/planeline/36 bool37 findIntersection(const Vector4& pt1, const Vector4& pt2, const Vector4& plane, Vector4& ret)38 {39 float a = plane.x;40 float b = plane.y;41 float c = plane.z;42 float d = plane.w;43 44 Vector4 p1 = pt1;45 p1.perspectiveDivide();46 float x1 = p1.x;47 float y1 = p1.y;48 float z1 = p1.z;49 50 Vector4 p2 = pt2;51 p2.perspectiveDivide();52 float x2 = p2.x;53 float y2 = p2.y;54 float z2 = p2.z;55 56 float uDenom = a * (x1 - x2) + b * (y1 - y2) + c * (z1 - z2);57 float uNumer = a * x1 + b * y1 + c * z1 + d;58 59 if (uDenom == 0){60 //plane parallel to line61 ERROR("Unexpected code path: ConvexPolygon.cpp\n");62 if (uNumer == 0){63 ret = p1;64 return true;65 }66 return false;67 }68 69 float u = uNumer / uDenom;70 ret.x = x1 + u * (x2 - x1);71 ret.y = y1 + u * (y2 - y1);72 ret.z = z1 + u * (z2 - z1);73 ret.w = 1;74 return true;75 }76 77 28 void 78 29 ConvexPolygon::transform(const Mat4x4& mat) … … 99 50 } 100 51 101 void 52 #define SIGN_DIFFERS(x, y) \ 53 ( ((x) < 0.0 && (y) > 0.0) || ((x) > 0.0 && (y) < 0.0) ) 54 55 bool 102 56 ConvexPolygon::clip(Plane& clipPlane, bool copyToTexcoord) 103 57 { 104 58 if (vertices.size() == 0) { 105 //ERROR("ConvexPolygon: polygon has no vertices\n");106 return ;59 ERROR("polygon has no vertices\n"); 60 return false; 107 61 } 108 62 109 63 VertexVector clippedVerts; 110 64 clippedVerts.reserve(2 * vertices.size()); … … 112 66 // The algorithm is as follows: for each vertex 113 67 // in the current poly, check to see which 114 // half space it is in: clipped or retained.68 // half space it is in: clipped (outside) or inside. 115 69 // If the previous vertex was in the other 116 // half space, find the intersect of that edge117 // with the clip plane and add that intersect 70 // half space, find the intersection of that edge 71 // with the clip plane and add that intersection 118 72 // point to the new list of vertices. If the 119 // current vertex is in the retainedhalf-space,73 // current vertex is in the inside half-space, 120 74 // add it to the new list as well. 121 75 122 Vector4 intersect;123 76 Vector4 plane = clipPlane.getCoeffs(); 124 77 125 bool prevRetained = isRetained(vertices[0], plane); 126 if (prevRetained) 127 clippedVerts.push_back(vertices[0]); 128 129 for (unsigned int i = 1; i < vertices.size(); i++) { 130 bool retained = isRetained(vertices[i], plane); 131 if (retained != prevRetained) { 132 bool found = findIntersection(vertices[i - 1], vertices[i], 133 plane, intersect); 134 assert(found); 135 clippedVerts.push_back(intersect); 78 // This implementation is based on the Mesa 3D library (MIT license) 79 int v1 = 0; 80 // dot product >= 0 on/inside half-space, < 0 outside half-space 81 float dot1 = vertices[v1] * plane; 82 for (unsigned int i = 1; i <= vertices.size(); i++) { 83 int v2 = (i == vertices.size()) ? 0 : i; 84 float dot2 = vertices[v2] * plane; 85 if (dot1 >= 0.0) { 86 // on/inside 87 clippedVerts.push_back(vertices[v1]); 136 88 } 137 if (retained) { 138 clippedVerts.push_back(vertices[i]); 89 if (SIGN_DIFFERS(dot1, dot2)) { 90 if (dot1 < 0.0) { 91 // outside -> inside 92 double t = dot1 / (dot1 - dot2); 93 clippedVerts.push_back(vlerp(vertices[v1], vertices[v2], t)); 94 } else { 95 // inside -> outside 96 double t = dot2 / (dot2 - dot1); 97 clippedVerts.push_back(vlerp(vertices[v2], vertices[v1], t)); 98 } 139 99 } 140 prevRetained = retained; 141 } 142 143 bool retained = isRetained(vertices[0], plane); 144 if (retained != prevRetained) { 145 bool found = findIntersection(vertices[vertices.size() - 1], 146 vertices[0], plane, intersect); 147 assert(found); 148 clippedVerts.push_back(intersect); 100 dot1 = dot2; 101 v1 = v2; 149 102 } 150 103 151 104 vertices.clear(); 152 vertices.insert(vertices.begin(),153 clippedVerts.begin(),154 clippedVerts.end());155 105 156 if (copyToTexcoord) 157 copyVerticesToTexcoords(); 106 if (clippedVerts.size() < 3) { 107 texcoords.clear(); 108 return false; 109 } else { 110 vertices.insert(vertices.begin(), 111 clippedVerts.begin(), 112 clippedVerts.end()); 113 114 if (copyToTexcoord) 115 copyVerticesToTexcoords(); 116 117 return true; 118 } 158 119 } 120 121 #undef SIGN_DIFFERS 159 122 160 123 void … … 180 143 glVertex4fv((float *)&(vertices[i])); 181 144 } 182 } 145 } else { 146 WARN("No polygons to render\n"); 147 } 183 148 } 184 149 … … 199 164 glVertex4fv((float *)(&tmp)); 200 165 } 166 } else { 167 WARN("No polygons to render\n"); 201 168 } 202 169 }
Note: See TracChangeset
for help on using the changeset viewer.