Geom Software - C++ Programming and Geometry Libraries
WOF Documentation pages v1.19
Point3.h
1 // Copyright (C) Geom Software e.U, Bernhard Kornberger, Graz/Austria
2 //
3 // This file is part of the WOF software. WOF is commercial software.
4 // Users holding a license may use this file in accordance with the
5 // License Agreement.
6 //
7 // This software is provided AS IS with NO WARRANTY OF ANY KIND,
8 // INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
9 // FOR A PARTICULAR PURPOSE.
10 //
11 // Please contact the author if any conditions of this licensing are
12 // not clear to you.
13 //
14 // Author: Bernhard Kornberger, bkorn (at) geom.at
15 // http://www.geom.at
16 
17 
18 #pragma once
19 
20 #include <math.h>
21 #include "Vector3.h"
22 #include <sstream>
23 namespace GEOM_WOF {
24 
25 
26 class Triangle2; // FWD
27 
30 class CLASS_DECLSPEC Point3
31 {
32 public:
33 
38 Point3(const double x_,const double y_,const double z_):
39  coordX(x_),
40  coordY(y_),
41  coordZ(z_),
42  r(0),g(0),b(0),a(0),bHasColor(false)
43 {
44 }
45 
51 Point3(const double x_,const double y_,const double z_,unsigned char r_,unsigned char g_,unsigned char b_,unsigned char a_):
52  coordX(x_),
53  coordY(y_),
54  coordZ(z_),
55  r(r_),
56  g(g_),
57  b(b_),
58  a(a_),
59  bHasColor(true)
60 {
61 }
62 
63 
68 Point3():coordX(0),coordY(0),coordZ(0),r(0),g(0),b(0),a(0),bHasColor(false)
69 {
70 }
71 
72 
77 Point3(const Point3& p_):
78  coordX(p_.coordX),
79  coordY(p_.coordY),
80  coordZ(p_.coordZ),
81  r(p_.r),g(p_.g),b(p_.b),a(p_.a),bHasColor(p_.bHasColor)
82 {
83 }
84 
89 Point3 &operator=(const Point3& other)
90 {
91  coordX=other.x();
92  coordY=other.y();
93  coordZ=other.z();
94  r=other.r;
95  g=other.g;
96  b=other.b;
97  a=other.a;
98  bHasColor=other.bHasColor;
99  return *this;
100 }
101 
105 {
106 }
107 
112 void setColor(unsigned char r_,unsigned char g_,unsigned char b_,unsigned char a_)
113 {
114  r=r_;
115  g=g_;
116  b=b_;
117  a=a_;
118  bHasColor=true;
119 }
120 
121 
122 
127 void getColor_255(unsigned char& r_,unsigned char& g_,unsigned char& b_,unsigned char& a_) const
128 {
129  r_=r;
130  g_=g;
131  b_=b;
132  a_=a;
133 }
134 
135 
140 void getColor_255(float& r_,float& g_,float& b_,float& a_) const
141 {
142  r_=static_cast<float>(r);
143  g_=static_cast<float>(g);
144  b_=static_cast<float>(b);
145  a_=static_cast<float>(a);
146 }
147 
152 std::string getColorString()
153 {
154  float r,g,b,a;
155  getColor_1(r,g,b,a);
156  std::stringstream ss;
157  ss<<r<<" "<<g<<" "<<b<<" "<<a;
158  return ss.str();
159 }
160 
165 void getColor_1(float& r_,float& g_,float& b_,float& a_) const
166 {
167  r_=r/255.0f;
168  g_=g/255.0f;
169  b_=b/255.0f;
170  a_=a/255.0f;
171 }
176 bool hasColor() const
177 {
178  return bHasColor;
179 }
180 
185 double x() const
186 {
187  return coordX;
188 }
189 
190 
195 double y() const
196 {
197  return coordY;
198 }
199 
200 
205 double z() const
206 {
207  return coordZ;
208 }
209 
210 
218 void xyz(double& x_,double& y_,double& z_) const
219 {
220  x_=coordX;
221  y_=coordY;
222  z_=coordZ;
223 }
224 
225 
226 
231 inline void addOwnCoords(double& x,double& y,double& z) const
232 {
233  x+=coordX;
234  y+=coordY;
235  z+=coordZ;
236 }
237 
243 inline void addOwnCoordsW(double& x,double& y,double& z,double w) const
244 {
245  x+=w*coordX;
246  y+=w*coordY;
247  z+=w*coordZ;
248 }
249 
251 // Internal use: add the point's color to r_,g_,b_,a_
252 inline void addOwnColor(float& r_,float& g_,float& b_,float& a_) const
253 {
254  r_+=float(r);
255  g_+=float(g);
256  b_+=float(b);
257  a_+=float(a);
258 }
259 
261 // Internal use: add the point's color to r_,g_,b_,a_
262 inline void addOwnColorW(float w,float& r_,float& g_,float& b_,float& a_) const
263 {
264  r_+=w*float(r);
265  g_+=w*float(g);
266  b_+=w*float(b);
267  a_+=w*float(a);
268 }
269 
270 
275 inline void copyColorFrom(const Point3& other)
276 {
277  r=other.r;
278  g=other.g;
279  b=other.b;
280  a=other.a;
281  bHasColor=other.bHasColor;
282 }
283 
284 
289 inline void copyColorFrom(const Point3& other0,const Point3& other1)
290 {
291  if(other0.hasColor() && other1.hasColor())
292  {
293  r=static_cast<unsigned char>(.5*(float(other0.r)+float(other1.r))+.5f);
294  g=static_cast<unsigned char>(.5*(float(other0.g)+float(other1.g))+.5f);
295  b=static_cast<unsigned char>(.5*(float(other0.b)+float(other1.b))+.5f);
296  a=static_cast<unsigned char>(.5*(float(other0.a+other1.a))+.5f);
297  bHasColor=true;
298  return;
299  }
300  if(other0.hasColor())
301  {
302  copyColorFrom(other0);
303  return;
304  }
305  if(other1.hasColor())
306  {
307  copyColorFrom(other0);
308  return;
309  }
310  bHasColor=false;
311 }
312 
318 inline void addWeightedOwnCoords(double weight,double& x,double& y,double& z) const
319 {
320  x+=(weight*coordX);
321  y+=(weight*coordY);
322  z+=(weight*coordZ);
323 }
324 
331  bool operator<(const Point3& p) const
332  {
333  if(coordX<p.coordX) return true;
334  if(coordX>p.coordX) return false;
335  if(coordY<p.coordY) return true;
336  if(coordY>p.coordY) return false;
337  return coordZ<p.coordZ;
338  }
339 
347  bool operator>(const Point3& p) const
348  {
349  if(coordX>p.coordX) return true;
350  if(coordX<p.coordX) return false;
351  if(coordY>p.coordY) return true;
352  if(coordY<p.coordY) return false;
353  return coordY>p.coordY;
354  }
359  bool operator==(const Point3& p) const
360  {
361  if(coordX==p.coordX && coordY==p.coordY && coordZ==p.coordZ) return true;
362  return false;
363  }
368 bool operator!=(const Point3& p) const
369 {
370  return (coordX!=p.coordX || coordY!=p.coordY || coordZ!=p.coordZ);
371 }
372 
373 
380 void set(const double x_,const double y_,const double z_)
381 {
382  coordX=x_;
383  coordY=y_;
384  coordZ=z_;
385 }
386 
393 void set(const Point3& pnt)
394 {
395  coordX=pnt.x();
396  coordY=pnt.y();
397  coordZ=pnt.z();
398 }
399 
400 
401 
402 
408 Vector3 operator-(const Point3& other) const
409 {
410  double xdiff(x()-other.x());
411  double ydiff(y()-other.y());
412  double zdiff(z()-other.z());
413  return Vector3(xdiff,ydiff,zdiff);
414 }
415 
420 Point3 operator+(const Vector3& vec) const
421 {
422  return Point3(x()+vec.x(),y()+vec.y(),z()+vec.z());
423 }
424 
425 
430 Point3 operator-(const Vector3& vec) const
431 {
432  return Point3(x()-vec.x(),y()-vec.y(),z()-vec.z());
433 }
434 
435 
436 friend std::ostream &operator<<(std::ostream &stream, const Point3& pnt);
437 friend std::istream &operator>>(std::istream &stream, Point3& pnt);
438 
439 
440 protected:
441  double coordX;
442  double coordY;
443  double coordZ;
444 public:
445  unsigned char r,g,b,a; // Color 0-255
446  bool bHasColor;
447 };
448 
449 
450 
451 
452 
453 
454 
455 
456 // Free functions
457 inline std::ostream &operator<<(std::ostream &stream, const Point3& pnt)
458 {
459  if(pnt.hasColor())
460  {
461  stream << "Point3 ("<<&pnt<<"): "<<pnt.x()<<", "<<pnt.y()<<", "<<pnt.z()<<", color("<<int(pnt.r)<<","<<int(pnt.g)<<","<<int(pnt.b)<<","<<int(pnt.a)<<")";
462  }
463  else
464  {
465  stream << "Point3 ("<<&pnt<<"): "<<pnt.x()<<", "<<pnt.y()<<", "<<pnt.z()<<", no color";
466  }
467  return stream;
468 }
469 
470 inline std::istream &operator>>(std::istream &stream, Point3& pnt)
471 {
472  stream >> pnt.coordX >> pnt.coordY >> pnt.coordZ;
473  return stream;
474 }
475 
476 
479 inline double sqDistance(const Point3& p0,const Point3& p1)
480 {
481  double deltaX=p1.x()-p0.x();
482  double deltaY=p1.y()-p0.y();
483  double deltaZ=p1.z()-p0.z();
484  return (deltaX*deltaX+deltaY*deltaY+deltaZ*deltaZ);
485 }
486 
489 inline double sqDistance(const Point3* p0,const Point3* p1)
490 {
491  double deltaX=p1->x()-p0->x();
492  double deltaY=p1->y()-p0->y();
493  double deltaZ=p1->z()-p0->z();
494  return (deltaX*deltaX+deltaY*deltaY+deltaZ*deltaZ);
495 }
496 
497 
500 inline double distance(const Point3& p0,const Point3& p1)
501 {
502  double deltaX=p1.x()-p0.x();
503  double deltaY=p1.y()-p0.y();
504  double deltaZ=p1.z()-p0.z();
505  return sqrt(deltaX*deltaX+deltaY*deltaY+deltaZ*deltaZ);
506 }
507 
508 
511 inline
512 Point3 center(const Point3& p0,const Point3& p1)
513 {
514  Point3 center((p0.x()+p1.x())/2.0,(p0.y()+p1.y())/2.0,(p0.z()+p1.z())/2.0);
515  return center;
516 }
517 
518 
519 } // (namespace)
3D Point
Definition: Point3.h:31
bool operator<(const Point3 &p) const
Less than operator.
Definition: Point3.h:331
void copyColorFrom(const Point3 &other)
Copy the color from another point.
Definition: Point3.h:275
double x() const
Get the x-coordinate.
Definition: Point3.h:185
Point3 & operator=(const Point3 &other)
operator=
Definition: Point3.h:89
Point3 operator-(const Vector3 &vec) const
operator-
Definition: Point3.h:430
double y() const
Get the y-coordinate.
Definition: Point3.h:195
void addOwnCoordsW(double &x, double &y, double &z, double w) const
Add the point's coordinates to x,y,z.
Definition: Point3.h:243
Point3()
Default constructor.
Definition: Point3.h:68
Vector3 operator-(const Point3 &other) const
operator-
Definition: Point3.h:408
void set(const double x_, const double y_, const double z_)
Set the coordiantes.
Definition: Point3.h:380
bool operator!=(const Point3 &p) const
Inequality operator.
Definition: Point3.h:368
Point3 operator+(const Vector3 &vec) const
operator+
Definition: Point3.h:420
void addOwnCoords(double &x, double &y, double &z) const
Add the point's coordinates to x,y,z.
Definition: Point3.h:231
~Point3()
Destructor.
Definition: Point3.h:104
Point3(const double x_, const double y_, const double z_, unsigned char r_, unsigned char g_, unsigned char b_, unsigned char a_)
Constructor with color.
Definition: Point3.h:51
void xyz(double &x_, double &y_, double &z_) const
Get the x-, y- and z-coordinate.
Definition: Point3.h:218
void set(const Point3 &pnt)
Set the coordiantes.
Definition: Point3.h:393
void getColor_255(unsigned char &r_, unsigned char &g_, unsigned char &b_, unsigned char &a_) const
Get color (unsigned char, range=0, ..., 255)
Definition: Point3.h:127
bool hasColor() const
Has color.
Definition: Point3.h:176
bool operator>(const Point3 &p) const
Greater than operator.
Definition: Point3.h:347
bool operator==(const Point3 &p) const
Equality operator.
Definition: Point3.h:359
void copyColorFrom(const Point3 &other0, const Point3 &other1)
Copy the average color from two points.
Definition: Point3.h:289
double z() const
Get the z-coordinate.
Definition: Point3.h:205
void addWeightedOwnCoords(double weight, double &x, double &y, double &z) const
Add the point's weighted coordinates to x,y,z.
Definition: Point3.h:318
void getColor_1(float &r_, float &g_, float &b_, float &a_) const
Get color (float, range=0.0, ..., 1.0)
Definition: Point3.h:165
void getColor_255(float &r_, float &g_, float &b_, float &a_) const
Get color (float, range=0, ..., 255)
Definition: Point3.h:140
Point3(const Point3 &p_)
Copy constructor.
Definition: Point3.h:77
Point3(const double x_, const double y_, const double z_)
Constructor without color.
Definition: Point3.h:38
std::string getColorString()
Get color string (string, range=0.0, ..., 1.0)
Definition: Point3.h:152
void setColor(unsigned char r_, unsigned char g_, unsigned char b_, unsigned char a_)
Set color.
Definition: Point3.h:112
3D Vector
Definition: Vector3.h:27
double z() const
Get the z-value.
Definition: Vector3.h:112
double x() const
Get the x-value.
Definition: Vector3.h:93
double y() const
Get the y-value.
Definition: Vector3.h:102
Definition: Point3.h:23
double sqDistance(const Point3 &p0, const Point3 &p1)
Get the squared distance between two points.
Definition: Point3.h:479
Point3 center(const Point3 &p0, const Point3 &p1)
Midpoint of p0 and p1.
Definition: Point3.h:512
double distance(const Point3 &p0, const Point3 &p1)
Get the distance between two points.
Definition: Point3.h:500