473,569 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c++ class struct and hell on earth

12 New Member
Hi,

I have made a few classes in c++. They somehow cooperate doing some 3d stuff.
Basically it is a moving camera acting as a flight, i have placed a lot of objects around the scene together with a b-spline surfcae.

The problem class is the polygon class. this class read 3d files generates faces, edges and so on.

here are two of the building blocks(structs)

Expand|Select|Wrap|Line Numbers
  1. struct FACE{
  2.     int num;        // index 
  3.     int index[3];          // index to 3 vertices
  4.     vector3 normal;    //normal vector to face
  5.  
  6. };
  7.  
  8.  
  9.  
  10. struct EDGE{
  11.  
  12.     int s_node;        //start vertex
  13.     int e_node;        //end vertex
  14.     FACE* adjface1;       //adjacent face1;
  15.     FACE* adjface2;       //adjacent face2;
  16.  
  17. };
  18.  
  19. struct VERTEX{
  20.     double x,y,z;        // Vertices coordinates
  21.     vector3 normal;       //vertex normal
  22. };
  23.  
  24.  
  25.  
  26.  
Only the EDGE struct is making trouble(i think)

I save these struct in their own vector, but it seems like something goes terrible wrong with this edge struct. When i later on call a method from outside this class that uses this vector of EDGE struct the program crashes after listing a 120 of 1600 edge structs from the vector. At this point i am doing this listing cout<<edges[c].adjface1->normal.x and so on only to debug.
edges is here the vector containing all the EDGE structs.

I have checked this print right after constructing the EDGE structs and everythig
is right. In fact i am using this struct to compute the vertex normals so i know it work when i call it from within the class.

The method i call from outside the class crash with a test 3d object after printing 120 of 1500 edges(the debug window in visual c++ opens) but if i call this method from within the class ,right after making the edges it list correctly all 1500 edges.

This is bad news because i have to call this method everytime something moves in the scene to get the contour edges of the objects.

Does anyone have any idees what is going on ? Could it be anything with the pointers to faces ? Should i make them as indicies instead ?
The vertex vector stores all the vertices and face struct adresses them with indicies to the vertex vector instead of pointers to structs.

The method calcSilEdges() is the one i mean. Calling this from outside dont work well only a few prints appear before debug window opens.

The Poly class object are saved in a vector outside this class so i can call each objects draw method.

Here is the poly class (stripped a lot)but i must say it is very hard to read.


Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. #include "Poly.h"
  4.  
  5. Poly::Poly(string s){
  6.  
  7.     LoadFile(s);    
  8.     }     
  9.  
  10.  
  11. void Poly:: LoadFile(string s){
  12.  
  13.  
  14.  
  15.         normalizeVertices();
  16.         calculateFaceNormals();
  17.     }
  18.  
  19. void Poly::makeVertexFacesArray(){
  20.  
  21.  
  22.  
  23. }
  24.  
  25. void Poly::calculateFaceNormals(){
  26.  
  27.  
  28.     makeEdges();
  29.     for(int i=0; i<numberOfFaces; i++){
  30.  
  31.             //vector 1 of face    
  32.             vec1=vec1.set((vertices[faces[i].index[1]].x-vertices[faces[i].index[0]].x),(vertices[faces[i].index[1]].y-vertices[faces[i].index[0]].y),(vertices[faces[i].index[1]].z-vertices[faces[i].index[0]].z));
  33.             //vector 2 of face
  34.             vec2=vec2.set(vertices[faces[i].index[2]].x-vertices[faces[i].index[0]].x,vertices[faces[i].index[2]].y-vertices[faces[i].index[0]].y,vertices[faces[i].index[2]].z-vertices[faces[i].index[0]].z);
  35.  
  36.             vec3=vec3.cross(vec1,vec2);
  37.  
  38.             //normalize
  39.             vec3=vec3.normalize(vec3);
  40.  
  41.             // save in struct
  42.             faces[i].normal=vec3;
  43.  
  44.  
  45.     }
  46.                 glVertex3f(vertices[faces[i].index[j]].x,vertices[faces[i].index[j]].y,vertices[faces[i].index[j]].z);
  47.  
  48.  
  49.     vector<int> face;
  50.     vector<int>::iterator it;
  51.  
  52.  
  53.     cout<<"number vert "<<numberOfFaces<<endl;
  54.             cout<<"number edges "<<edges.size()<<endl;
  55.  
  56.             for(int c=0;c<numberOfVertices;c++){
  57.                 cout<<".";
  58.                 normalvector.x=0;
  59.                 normalvector.y=0;
  60.                 normalvector.z=0;
  61.  
  62.                 float number=0;
  63.                 for(unsigned int cc=0;cc<edges.size();cc++){
  64.  
  65.                     if(c==edges[cc].s_node || c==edges[cc].e_node){
  66.  
  67.  
  68.                             int e1=edges[cc].adjface1->num;
  69.                             it=find(face.begin(),face.end(),e1);
  70.                             if(it==face.end()){
  71.                                 face.push_back(e1);
  72.                                 tempvector = edges[cc].adjface1->normal;
  73.                                 normalvector=normalvector.add(tempvector,normalvector);
  74.                                 number=number+1;
  75.                             }
  76.                             else{
  77.  
  78.                             }
  79.  
  80.  
  81.                             if(edges[cc].adjface2!=NULL){
  82.                             int e2=edges[cc].adjface2->num;
  83.                             it=find(face.begin(),face.end(),e2);
  84.                             if(it==face.end()){
  85.                                 //cout<<"face "<<e2<<" var ikke i listen"<<endl;
  86.                                 face.push_back(e2);
  87.                                 tempvector = edges[cc].adjface2->normal;
  88.                                 normalvector=normalvector.add(tempvector,normalvector);
  89.                                 number=number+1;
  90.                             }
  91.                             else{
  92.  
  93.                             }
  94.  
  95.                             }            
  96.                     }
  97.  
  98.                 }
  99.             normalvector=normalvector.mulSkalar(normalvector,(1/number));
  100.  
  101.             vertices[c].normal=normalvector;
  102.                 face.clear();
  103.  
  104.             }
  105.  
  106.  
  107.  
  108.             //calcSilEdges(lpos);  // this works
  109.  
  110.  
  111. }
  112.  
  113.  
  114. void Poly::Draw(){
  115.  
  116.     glColor3f(c.r,c.g,c.b);
  117.     for(int i=0; i<numberOfFaces; i++){
  118.  
  119.         glBegin(GL_TRIANGLES);
  120.  
  121.         for(int j=0; j<3; j++){
  122.             glNormal3f(vertices[faces[i].index[j]].normal.x,vertices[faces[i].index[j]].normal.y,vertices[faces[i].index[j]].normal.z);                    glVertex3f(vertices[faces[i].index[j]].x,vertices[faces[i].index[j]].y,vertices[faces[i].index[j]].z);
  123.         }
  124.  
  125.     glEnd();
  126.  
  127.  
  128.     }
  129.  
  130. }
  131.  
  132.  
  133. void Poly::normalizeVertices(){
  134.  
  135.     for (unsigned int c=0;c<vertices.size();c++)
  136.     {
  137.         vertices[c].x=(vertices[c].x)/maxvalue;
  138.         vertices[c].y=(vertices[c].y)/maxvalue;
  139.         vertices[c].z=(vertices[c].z)/maxvalue;
  140.     }
  141.  
  142.  
  143. }
  144.  
  145. void Poly::setPoints(float x1, float y1, float z1){
  146. v.x=x1;
  147. v.y=y1;
  148. v.z=z1;
  149. vertices.push_back(v);
  150.  
  151.  
  152. }
  153.  
  154.  
  155. // method to triangulate surface
  156. void Poly::makeFaces(int row, int col, float detail){
  157.     int i=0;
  158.  
  159.  
  160.     int mod = (int)ceil((1/detail));
  161.  
  162.     TEMPARRAY temp;
  163.     TEMPARRAY temp2;
  164.  
  165.     vector<TEMPARRAY> arrays; //vector to keep row number arrays
  166.     vector<TEMPARRAY> arrays2;
  167.  
  168.     //init vertices idices
  169.     int counter2=0;
  170.  
  171.     cout<<"mod "<<mod<<endl;
  172.     cout<<"col "<<col<<endl;
  173.  
  174.  
  175.  
  176.     for(int c=0;c<(row-3);c++){
  177.  
  178.         for(int counter=0;counter<(mod*mod*(col-3));counter++){
  179.  
  180.             temp.tempA.push_back(counter2);        
  181.  
  182.         counter2++;
  183.         }
  184.  
  185.     arrays.push_back(temp);
  186.     temp.tempA.clear();
  187.  
  188.     }
  189.  
  190.     for(int c=0;c<(row-3);c++){
  191.  
  192.         for(int counter=0;counter<(mod*mod*(col-3))-(mod+1);counter++){
  193.  
  194.             if((counter+1)%mod==0){
  195.                 if (counter<vertices.size()-(mod+1)){
  196.                     counter=counter+1;
  197.                 }
  198.             }
  199.         f.index[0]=arrays[c].tempA[counter];
  200.         f.index[1]=arrays[c].tempA[counter+1];
  201.         f.index[2]=arrays[c].tempA[counter+mod];
  202.         f.num=numberOfFaces;
  203.         faces.push_back(f);
  204.         numberOfFaces=numberOfFaces+1;
  205.         f.index[0]=arrays[c].tempA[counter+1];
  206.         f.index[1]=arrays[c].tempA[counter+mod+1];
  207.         f.index[2]=arrays[c].tempA[counter+mod];
  208.         f.num=numberOfFaces;
  209.         faces.push_back(f);
  210.         numberOfFaces=numberOfFaces+1;
  211.         }
  212.  
  213.     }
  214.     int flag=0;
  215.     for(int c=0;c<(row-3);c++){
  216.  
  217.         for(int counter=0;counter<(mod*mod*(col-3));counter++){
  218.  
  219.             if(c==0){
  220.  
  221.                 if((counter+1)%mod==0){
  222.                     temp.tempA.push_back(arrays[c].tempA[counter]);
  223.  
  224.                 }    
  225.             }
  226.             if(c==(row-4)){
  227.                 if((counter)%mod==0){
  228.                     temp2.tempA.push_back(arrays[c].tempA[counter]);
  229.                     flag=2;
  230.                 }
  231.             }
  232.  
  233.  
  234.             if ((c!=0) & (c!=(row-4))){
  235.  
  236.                 if((counter+1)%mod==0){
  237.                     temp.tempA.push_back(arrays[c].tempA[counter]);
  238.                     flag=1;
  239.                 }    
  240.                 if((counter)%mod==0){
  241.                     temp2.tempA.push_back(arrays[c].tempA[counter]);
  242.                     flag=1;
  243.                 }
  244.             }
  245.  
  246.         }
  247.         if(flag==0){
  248.             arrays2.push_back(temp);
  249.         }
  250.         if(flag==1){
  251.             arrays2.push_back(temp2);
  252.             arrays2.push_back(temp);
  253.         }
  254.         if(flag==2){
  255.             arrays2.push_back(temp2);
  256.         }
  257.         temp.tempA.clear();
  258.         temp2.tempA.clear();
  259.  
  260.     }
  261.  
  262.     for(int c=0;c<(arrays2.size()-1);c++){
  263.  
  264.         for(int counter=0;counter<((mod*(col-3))-1);counter++){
  265.  
  266.  
  267.  
  268.         f.index[0]=arrays2[c].tempA[counter];
  269.         f.index[1]=arrays2[c+1].tempA[counter];
  270.         f.index[2]=arrays2[c].tempA[counter+1];
  271.         f.num=numberOfFaces;
  272.         faces.push_back(f);
  273.         numberOfFaces=numberOfFaces+1;
  274.         f.index[0]=arrays2[c+1].tempA[counter];
  275.         f.index[1]=arrays2[c+1].tempA[counter+1];
  276.         f.index[2]=arrays2[c].tempA[counter+1];
  277.         f.num=numberOfFaces;
  278.         faces.push_back(f);
  279.         numberOfFaces=numberOfFaces+1;
  280.  
  281.         }
  282.     }
  283.  
  284.  
  285.  
  286.         }
  287.  
  288.  
  289. }
  290.  
  291. void Poly::makeEdges(){
  292.  
  293.     for(int c=0;c<faces.size();c++){
  294.  
  295.         e.s_node = faces[c].index[0];
  296.         e.e_node = faces[c].index[1];
  297.  
  298.         e.adjface1 = &faces[c];
  299.         e.adjface2 = NULL;
  300.         edges.push_back(e);
  301.  
  302.         e.s_node = faces[c].index[1];
  303.         e.e_node = faces[c].index[2];
  304.  
  305.         e.adjface1 = &faces[c];
  306.         e.adjface2 = NULL;
  307.         edges.push_back(e);
  308.  
  309.         e.s_node = faces[c].index[2];
  310.         e.e_node = faces[c].index[0];
  311.  
  312.         e.adjface1 = &faces[c];
  313.         e.adjface2 = NULL;
  314.         edges.push_back(e);
  315.  
  316.     }
  317.  
  318.     for(int c=0;c<edges.size();c++){
  319.  
  320.         int start=edges[c].s_node;
  321.         int end=edges[c].e_node;
  322.  
  323.         for(int cc=0;cc<edges.size();cc++){
  324.  
  325.             if(start==edges[cc].s_node || start==edges[cc].e_node){
  326.  
  327.                 if(end==edges[cc].s_node || end==edges[cc].e_node){
  328.  
  329.                     if((start!=edges[cc].s_node) & (end!=edges[cc].e_node)){
  330.  
  331.  
  332.                         edges[c].adjface2=edges[cc].adjface1;
  333.                         edges.erase(edges.begin()+cc);    
  334.                         //cout<<"edges size "<<edges.size()<<endl;
  335.                     }
  336.                 }
  337.             }
  338.         }
  339. }
  340.  
  341. }
  342.  
  343. }
  344.  
  345. void Poly::calcSilEdges(float pos[]){
  346.     lpos[0]=pos[0];
  347.     lpos[1]=pos[1];
  348.     lpos[2]=pos[2];
  349.  
  350.     for(unsigned int c=0;c<edges.size();c++){
  351.  
  352.         cout<<edges[c].adjface1->normal.x<<endl;
  353.  
  354.         }
  355.  
  356.  
  357. }
  358.  
Oct 30 '06 #1
1 2245
stromhau
12 New Member
Ok, did a change to the EDGE struct :

struct EDGE {


int s_node; //startnode
int e_node; //end node
int adjface1; //adjacent face1;
int adjface2; //adjacent face2;

};

Now struct FACE is stored in a vector and adjface1 and adjface2 is indicies faces to this vector.

What was wrong with pointer code ?

Tommy,
Oct 30 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
11230
by: SACHIN | last post by:
I have this class as part of a Consol application. using System; namespace Bugreport { /// <summary> /// This class tries to use the Class/Struct combination. /// </summary> class Class1 {
7
2121
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
11
1671
by: Robert Sturzenegger | last post by:
We had a strange problem in a large project with the use of a string class in a static instance of a struct. I managed to reproduce the exactly same problem in a very short program (see below). When the constant SHORT_STRING_SIZE_LIMIT is set to 0, the program works as expected and outputs the string "+". When it is set to 7, it outputs...
258
8449
by: Terry Andersen | last post by:
If I have: struct one_{ unsigned int one_1; unsigned short one_2; unsigned short one_3; }; struct two_{ unsigned int two_1;
6
2330
by: JS | last post by:
I have this struct: struct pcb { void *(*start_routine) (void *); void *arg; jmp_buf state; int stak; }; These pointers:
1
7277
by: xahlee | last post by:
Elisp Tutorial: Make Google Earth Xah Lee, 2006-12 This page shows a example of writing a emacs lisp function that creates a Google Earth file, and creates a link to the file, as well a link to Google Map. If you don't know elisp, first take a gander at Elisp Basics. I often write travelogs on my website. If i traveled to Las Vegas,...
2
2295
by: Ninereeds | last post by:
I'm messing around with using mixin-layers (look for papers by Yannis Smaragdakis and Don Batory) to define data structures. One issue is that nodes tend to have pointers to other nodes - the pointers have to point to the full node type, and have to be referenced before that full node type is known. One solution is to use the 'fixpoint...
6
1867
by: jacek.dziedzic | last post by:
Hello! I have some legacy C code which expects a pointer to a function to be evaluated at one point. Because of the pointer-to-function vs. pointer-to-member incompatibility, this needs to be a global function. To aid the passing of some extra data to the function, it takes an extra parameter of type void* which is passed uninterpreted to...
25
2755
by: Jeremy Banks | last post by:
Hi. I wondered if anyone knew the rationale behind the naming of the Popen class in the subprocess module. Popen sounds like the a suitable name for a function that created a subprocess, but the object itself is a subprocess, not a "popen". It seems that it would be more accurate to just name the class Subprocess, can anyone explain why this...
0
7693
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7665
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5501
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.