Changeset 978 for trunk/vizservers/nanovis/ConvexPolygon.cpp
- Timestamp:
- Apr 2, 2008, 8:07:53 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/vizservers/nanovis/ConvexPolygon.cpp
r451 r978 1 1 2 /* 2 3 * ---------------------------------------------------------------------- … … 14 15 */ 15 16 #include "ConvexPolygon.h" 16 #include <GL/gl ut.h>17 #include <GL/gl.h> 17 18 #include <assert.h> 18 19 19 20 ConvexPolygon::ConvexPolygon(){} 20 21 21 ConvexPolygon::ConvexPolygon(VertexVector vertices){ 22 this->vertices.insert(this->vertices.begin(), 23 vertices.begin(), 24 vertices.end()); 25 } 26 27 void ConvexPolygon::set_id(int v_id) { volume_id = v_id; }; 28 29 void ConvexPolygon::append_vertex(Vector4 vert){ 30 vertices.push_back(vert); 31 } 32 33 void ConvexPolygon::insert_vertex(unsigned int index, Vector4 vert) { 34 assert(index<vertices.size()); 35 vertices.insert(vertices.begin() + index, vert); 36 } 37 38 bool is_retained(Vector4 point, Vector4 plane){ 39 return ( point * plane>=0 ) ; 22 ConvexPolygon::ConvexPolygon(VertexVector newvertices){ 23 vertices.insert(vertices.begin(), newvertices.begin(), newvertices.end()); 40 24 } 41 25 … … 48 32 // 49 33 // http://astronomy.swin.edu.au/pbourke/geometry/planeline/ 50 bool findIntersection(Vector4 p1, Vector4 p2, Vector4 plane, Vector4 &ret){ 51 float a = plane.x; 52 float b = plane.y; 53 float c = plane.z; 54 float d = plane.w; 55 56 p1.perspective_devide(); 34 bool 35 findIntersection(Vector4 p1, Vector4 p2, Vector4 plane, Vector4 &ret){ 36 float a = plane.x; 37 float b = plane.y; 38 float c = plane.z; 39 float d = plane.w; 40 41 p1.perspective_devide(); 57 42 float x1 = p1.x; 58 43 float y1 = p1.y; 59 44 float z1 = p1.z; 60 45 61 46 p2.perspective_devide(); 62 47 float x2 = p2.x; 63 48 float y2 = p2.y; … … 68 53 69 54 if (uDenom == 0){ 70 71 72 73 74 75 76 77 55 //plane parallel to line 56 fprintf(stderr, "Unexpected code path: ConvexPolygon.cpp\n"); 57 if (uNumer == 0){ 58 for (int i = 0; i < 4; i++) 59 ret < p1; 60 return true; 61 } 62 return false; 78 63 } 79 64 … … 83 68 ret.z = z1 + u * (z2 - z1); 84 69 ret.w = 1; 85 return true; 86 } 87 88 void ConvexPolygon::transform(Mat4x4 mat){ 89 VertexVector tmp = vertices; 90 vertices.clear(); 91 92 for (unsigned int i = 0; i < tmp.size(); i++) { 93 Vector4 vec = tmp[i]; 94 vertices.push_back(mat.transform(vec)); 95 } 96 } 97 98 99 void ConvexPolygon::translate(Vector4 shift){ 100 VertexVector tmp = vertices; 101 vertices.clear(); 102 103 for (unsigned int i = 0; i < tmp.size(); i++) { 104 Vector4 vec = tmp[i]; 105 vertices.push_back(vec+shift); 106 } 107 } 108 109 void ConvexPolygon::clip(Plane &clipPlane, bool copy_to_texcoord) { 110 if (vertices.size() == 0) { 111 //fprintf(stderr, "ConvexPolygon: polygon has no vertices\n"); 112 return; 113 } 70 return true; 71 } 72 73 void 74 ConvexPolygon::transform(Mat4x4 mat){ 75 VertexVector tmp = vertices; 76 vertices.clear(); 77 78 for (unsigned int i = 0; i < tmp.size(); i++) { 79 Vector4 vec = tmp[i]; 80 vertices.push_back(mat.transform(vec)); 81 } 82 } 83 84 85 void 86 ConvexPolygon::translate(Vector4 shift){ 87 VertexVector tmp = vertices; 88 vertices.clear(); 89 90 for (unsigned int i = 0; i < tmp.size(); i++) { 91 Vector4 vec = tmp[i]; 92 vertices.push_back(vec+shift); 93 } 94 } 95 96 void 97 ConvexPolygon::clip(Plane &clipPlane, bool copy_to_texcoord) { 98 if (vertices.size() == 0) { 99 //fprintf(stderr, "ConvexPolygon: polygon has no vertices\n"); 100 return; 101 } 114 102 115 103 VertexVector clippedVerts; … … 131 119 bool prevRetained = is_retained(vertices[0], plane); 132 120 if (prevRetained) 133 121 clippedVerts.push_back(vertices[0]); 134 122 135 123 for (unsigned int i = 1; i < vertices.size(); i++) { 136 bool retained = is_retained(vertices[i], plane); 137 if (retained != prevRetained) { 138 bool found = findIntersection(vertices[i - 1], vertices[i], 139 plane, intersect); 140 if (!found) 141 assert(false); 142 clippedVerts.push_back(intersect); 143 } 144 if (retained) { 145 clippedVerts.push_back(vertices[i]); 146 } 147 prevRetained = retained; 124 bool retained = is_retained(vertices[i], plane); 125 if (retained != prevRetained) { 126 bool found = findIntersection(vertices[i - 1], vertices[i], 127 plane, intersect); 128 assert(found); 129 clippedVerts.push_back(intersect); 130 } 131 if (retained) { 132 clippedVerts.push_back(vertices[i]); 133 } 134 prevRetained = retained; 148 135 } 149 136 150 137 bool retained = is_retained(vertices[0], plane); 151 138 if (retained != prevRetained) { 152 bool found = findIntersection(vertices[vertices.size() - 1], vertices[0], 153 plane, intersect); 154 if (!found) 155 assert(false); 156 clippedVerts.push_back(intersect); 139 bool found = findIntersection(vertices[vertices.size() - 1], 140 vertices[0], plane, intersect); 141 assert(found); 142 clippedVerts.push_back(intersect); 157 143 } 158 144 159 145 vertices.clear(); 160 146 vertices.insert(vertices.begin(), 161 162 147 clippedVerts.begin(), 148 clippedVerts.end()); 163 149 164 150 if(copy_to_texcoord) 165 copy_vertices_to_texcoords(); 166 } 167 168 void ConvexPolygon::copy_vertices_to_texcoords(){ 151 copy_vertices_to_texcoords(); 152 } 153 154 void 155 ConvexPolygon::copy_vertices_to_texcoords(){ 169 156 if(texcoords.size()>0) 170 texcoords.clear();157 texcoords.clear(); 171 158 172 159 for (unsigned int i=0; i<vertices.size(); i++) { 173 174 } 175 } 176 177 178 voidConvexPolygon::Emit(bool use_texture)160 texcoords.push_back(vertices[i]); 161 } 162 } 163 164 void 165 ConvexPolygon::Emit(bool use_texture) 179 166 { 180 if (vertices.size() >= 3) 181 { 182 for (unsigned int i = 0; i<vertices.size(); i++) 183 { 184 if(use_texture){ 185 glTexCoord4fv((float *)&(texcoords[i])); 186 //glTexCoord4fv((float *)&(vertices[i])); 187 } 188 glVertex4fv((float *)&(vertices[i])); 189 /* 190 //debug 191 fprintf(stderr, "(%f %f %f %f)", 192 vertices[i].x, 193 vertices[i].y, 194 vertices[i].z, 195 vertices[i].w ); 196 */ 197 } 198 } 199 //fprintf(stderr, " "); 200 } 201 202 203 void ConvexPolygon::Emit(bool use_texture, Vector3& shift, Vector3& scale) 167 if (vertices.size() >= 3) { 168 for (unsigned int i = 0; i<vertices.size(); i++) { 169 if(use_texture) { 170 glTexCoord4fv((float *)&(texcoords[i])); 171 //glTexCoord4fv((float *)&(vertices[i])); 172 } 173 glVertex4fv((float *)&(vertices[i])); 174 /* 175 //debug 176 fprintf(stderr, "(%f %f %f %f)", 177 vertices[i].x, 178 vertices[i].y, 179 vertices[i].z, 180 vertices[i].w ); 181 */ 182 } 183 } 184 //fprintf(stderr, " "); 185 } 186 187 188 void 189 ConvexPolygon::Emit(bool use_texture, Vector3& shift, Vector3& scale) 204 190 { 205 if (vertices.size() >= 3) 206 { 207 for (unsigned int i = 0; i<vertices.size(); i++) 208 { 209 if(use_texture) 210 glTexCoord4fv((float *)&(vertices[i])); 211 212 Vector4 tmp = (vertices[i]); 213 Vector4 shift_4d = Vector4(shift.x, shift.y, shift.z, 0); 214 tmp = tmp + shift_4d; 215 tmp.x = tmp.x * scale.x; 216 tmp.y = tmp.y * scale.y; 217 tmp.z = tmp.z * scale.z; 218 glVertex4fv((float *)(&tmp)); 219 } 220 } 221 } 191 if (vertices.size() >= 3) { 192 for (unsigned int i = 0; i<vertices.size(); i++) { 193 if(use_texture) { 194 glTexCoord4fv((float *)&(vertices[i])); 195 } 196 Vector4 tmp = (vertices[i]); 197 Vector4 shift_4d = Vector4(shift.x, shift.y, shift.z, 0); 198 tmp = tmp + shift_4d; 199 tmp.x = tmp.x * scale.x; 200 tmp.y = tmp.y * scale.y; 201 tmp.z = tmp.z * scale.z; 202 glVertex4fv((float *)(&tmp)); 203 } 204 } 205 }
Note: See TracChangeset
for help on using the changeset viewer.