Ignore:
Timestamp:
Apr 2, 2008, 8:07:53 AM (16 years ago)
Author:
gah
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/vizservers/nanovis/ConvexPolygon.cpp

    r451 r978  
     1 
    12/*
    23 * ----------------------------------------------------------------------
     
    1415 */
    1516#include "ConvexPolygon.h"
    16 #include <GL/glut.h>
     17#include <GL/gl.h>
    1718#include <assert.h>
    1819
    1920ConvexPolygon::ConvexPolygon(){}
    2021
    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 ) ; 
     22ConvexPolygon::ConvexPolygon(VertexVector newvertices){
     23    vertices.insert(vertices.begin(), newvertices.begin(), newvertices.end());
    4024}
    4125
     
    4832//
    4933// 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();
     34bool
     35findIntersection(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();
    5742    float x1 = p1.x;
    5843    float y1 = p1.y;
    5944    float z1 = p1.z;
    6045
    61         p2.perspective_devide();
     46    p2.perspective_devide();
    6247    float x2 = p2.x;
    6348    float y2 = p2.y;
     
    6853
    6954    if (uDenom == 0){
    70                 //plane parallel to line
    71                 fprintf(stderr, "Unexpected code path: ConvexPolygon.cpp\n");
    72                 if (uNumer == 0){
    73                         for (int i = 0; i < 4; i++)
    74                                 ret < p1;
    75                                 return true;
    76                 }
    77                 return false;
     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;
    7863    }
    7964
     
    8368    ret.z = z1 + u * (z2 - z1);
    8469    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
     73void
     74ConvexPolygon::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
     85void
     86ConvexPolygon::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
     96void
     97ConvexPolygon::clip(Plane &clipPlane, bool copy_to_texcoord) {
     98    if (vertices.size() == 0) {
     99        //fprintf(stderr, "ConvexPolygon: polygon has no vertices\n"); 
     100        return;
     101    }
    114102   
    115103    VertexVector clippedVerts;
     
    131119    bool prevRetained = is_retained(vertices[0], plane);
    132120    if (prevRetained)
    133                 clippedVerts.push_back(vertices[0]);
     121        clippedVerts.push_back(vertices[0]);
    134122
    135123    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;
    148135    }
    149136
    150137    bool retained = is_retained(vertices[0], plane);
    151138    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);
    157143    }
    158144
    159145    vertices.clear();
    160146    vertices.insert(vertices.begin(),
    161                     clippedVerts.begin(),
    162                     clippedVerts.end());
     147                    clippedVerts.begin(),
     148                    clippedVerts.end());
    163149
    164150    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
     154void
     155ConvexPolygon::copy_vertices_to_texcoords(){
    169156    if(texcoords.size()>0)
    170       texcoords.clear();
     157        texcoords.clear();
    171158
    172159    for (unsigned int i=0; i<vertices.size(); i++) {
    173         texcoords.push_back(vertices[i]);
    174     }
    175 }
    176 
    177 
    178 void ConvexPolygon::Emit(bool use_texture)
     160        texcoords.push_back(vertices[i]);
     161    }
     162}
     163
     164void
     165ConvexPolygon::Emit(bool use_texture)
    179166{
    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
     188void
     189ConvexPolygon::Emit(bool use_texture, Vector3& shift, Vector3& scale)
    204190{
    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.