My Project
basicComponents.h
Go to the documentation of this file.
1 #ifndef BASICCOMPONENTS_H
2 #define BASICCOMPONENTS_H
3 #include <vector>
4 #include <string>
5 #include <iostream>
6 #include <cmath>
7 #define EPS 0.01
8 using namespace std;
9 struct Vertice
10 {
11 
12 // public:
13  float first,second,third=0;
14  bool is3d=true;
15  char label;
16 
24  Vertice t;
25  t.first=first;
26  t.second=second;
27  t.third=third;
28  t.is3d=is3d;
29  return t;
30  }
31 
33  bool operator<(Vertice other) const
34  {
35 
36  if(is3d) return make_pair(first*100,make_pair(second*100,third*100)) > make_pair(other.first*100,make_pair(other.second*100,other.third*100));
37  else return make_pair(first*100,second*100) > make_pair(other.first*100,other.second*100);
38  }
39 
41  bool operator ==(Vertice b) {
42  return (abs(first - b.first)<EPS ) && (abs(second - b.second)<EPS ) && ( ( (abs(third - b.third)<EPS ) && b.is3d && is3d) || (!is3d) );
43  }
44 
46  Vertice operator-(const Vertice& rhs){
47  Vertice diff;
48  diff.first=first - rhs.first;
49  diff.second=second -= rhs.second;
50  if(is3d && rhs.is3d) {
51  diff.third=third-=rhs.third;
52  diff.is3d=true;
53  }
54  else{
55  diff.is3d=false;
56  }
57  return diff;
58  }
59 
60 
61 
62 };
63 
65 inline ostream& operator<<(std::ostream& os,const Vertice& v)
66 {
67  if(v.is3d) return os << v.first+50 << ", " << v.second+50 << ", " << v.third+50 << endl;
68  else return os << v.first+50 << ", " << v.second+50 <<endl;
69 }
70 
71 struct Edge {
72 
73 // public:
74  pair<Vertice,Vertice> vertices;
75 
77  bool operator<(Edge other) const
78  {
79  return vertices > other.vertices;
80  }
81 
83  bool operator ==(Edge b) {
84  return (vertices.first==b.vertices.first && vertices.second==b.vertices.second ) ||
85  ( vertices.first==b.vertices.second && vertices.second==b.vertices.first );
86  }
87 
97  Edge projected(int plane){
98  Vertice u=vertices.first, v=vertices.second, u_proj, v_proj;
99  u_proj.is3d=false;
100  v_proj.is3d=false;
101  double ux=u.first, uy=u.second, uz=u.third, vx=v.first, vy=v.second, vz=v.third;
102  Edge e;
103  if(plane==0) { //XY Plane
104  u_proj.first=ux;
105  u_proj.second=uy;
106  v_proj.first=vx;
107  v_proj.second=vy;
108  }
109  else if(plane==1) { //YZ Plane
110  u_proj.first=uy;
111  u_proj.second=uz;
112  v_proj.first=vy;
113  v_proj.second=vz;
114  }
115  else if(plane==2) { //XZ Plane
116  u_proj.first=ux;
117  u_proj.second=uz;
118  v_proj.first=vx;
119  v_proj.second=vz;
120  }
121  else if(plane==3) { //isometric view
122  u_proj.first= (std::sqrt(3) * ux - std::sqrt(3) * uz)/ std::sqrt(6);
123  u_proj.second= (ux + 2*uy + uz)/ std::sqrt(6);
124  v_proj.first= (std::sqrt(3) * vx - std::sqrt(3) * vz)/ std::sqrt(6);
125  v_proj.second= (vx + 2*vy + vz)/ std::sqrt(6);
126  //for reversing the projection
127  // u_proj.third=(ux-uy+uz)/std::sqrt(3);
128  // v_proj.third=(vx-vy+vz)/std::sqrt(3);
129  }
130  e.vertices.first=u_proj;
131  e.vertices.second=v_proj;
132  return e;
133 }
134 
135 
136 };
137 
138 
140 struct Plane {
141  float a,b,c,d;
142 
145  Vertice uv= v-u, uw=w-u;
146  a= uv.second*uw.third - uv.third*uw.second;
147  b= -uv.first*uw.third + uv.third*uw.first;
148  c= uv.first*uw.second - uv.second*uw.first;
149  d= -(a*u.first + b*u.second + c*u.third);
150  }
151 
152  float distance(Vertice u){
153  float x= sqrt(a*a +b*b +c*c);
154  float d1=(a*u.first + b*u.second + c*u.third + d);
155  return abs(d1/x);
156  }
157 
159  bool onPlane(Edge e){
160  Vertice u=e.vertices.first, v=e.vertices.second;
161  if(distance(u)<EPS && distance(v)<EPS) return true;
162  else return false;
163  }
164 };
165 
166 
167 #endif
bool is3d
Definition: basicComponents.h:14
ostream & operator<<(std::ostream &os, const Vertice &v)
Function to print shifted co-ordiantes of vertices for debugging.
Definition: basicComponents.h:65
Edge projected(int plane)
Definition: basicComponents.h:97
Vertice deepCopy()
Definition: basicComponents.h:23
char label
Definition: basicComponents.h:15
Definition: basicComponents.h:71
bool operator<(Vertice other) const
Less than operator defined for faster comparisons between different vertices.
Definition: basicComponents.h:33
Vertice operator-(const Vertice &rhs)
Difference operator defined for faster comparisons between different vertices.
Definition: basicComponents.h:46
bool operator<(Edge other) const
Less than operator defined for faster comparisons between Edges.
Definition: basicComponents.h:77
float second
Definition: basicComponents.h:13
float first
Definition: basicComponents.h:13
Definition: basicComponents.h:9
bool onPlane(Edge e)
Function used to check whether an Edge lies on the plane or not.
Definition: basicComponents.h:159
float d
Definition: basicComponents.h:141
#define EPS
Definition: basicComponents.h:7
float distance(Vertice u)
Definition: basicComponents.h:152
float third
Definition: basicComponents.h:13
pair< Vertice, Vertice > vertices
Definition: basicComponents.h:74
Plane(Vertice u, Vertice v, Vertice w)
Constructor to define plane using 3 vertices.
Definition: basicComponents.h:144
Structured Defined to represent Planes.
Definition: basicComponents.h:140