00001 /* 00002 This file is part of the VRender library. 00003 Copyright (C) 2005 Cyril Soler (Cyril.Soler@imag.fr) 00004 Version 1.0.0, released on June 27, 2005. 00005 00006 http://artis.imag.fr/Members/Cyril.Soler/VRender 00007 00008 VRender is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 2 of the License, or 00011 (at your option) any later version. 00012 00013 VRender is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with VRender; if not, write to the Free Software Foundation, Inc., 00020 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. 00021 */ 00022 00023 /**************************************************************************** 00024 00025 Copyright (C) 2002-2013 Gilles Debunne. All rights reserved. 00026 00027 This file is part of the QGLViewer library version 2.4.0. 00028 00029 http://www.libqglviewer.com - contact@libqglviewer.com 00030 00031 This file may be used under the terms of the GNU General Public License 00032 versions 2.0 or 3.0 as published by the Free Software Foundation and 00033 appearing in the LICENSE file included in the packaging of this file. 00034 In addition, as a special exception, Gilles Debunne gives you certain 00035 additional rights, described in the file GPL_EXCEPTION in this package. 00036 00037 libQGLViewer uses dual licensing. Commercial/proprietary software must 00038 purchase a libQGLViewer Commercial License. 00039 00040 This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00041 WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00042 00043 *****************************************************************************/ 00044 00045 #include <math.h> 00046 #include <assert.h> 00047 #include "Primitive.h" 00048 #include "Types.h" 00049 00050 using namespace vrender ; 00051 using namespace std ; 00052 00053 Point::Point(const Feedback3DColor& f) 00054 : _position_and_color(f) 00055 { 00056 } 00057 00058 const Vector3& Point::vertex(int) const 00059 { 00060 return _position_and_color.pos() ; 00061 } 00062 00063 const Feedback3DColor& Point::sommet3DColor(int) const 00064 { 00065 return _position_and_color ; 00066 } 00067 00068 const Feedback3DColor& Segment::sommet3DColor(int i) const 00069 { 00070 return ( (i&1)==0 )?P1:P2 ; 00071 } 00072 00073 AxisAlignedBox_xyz Point::bbox() const 00074 { 00075 return AxisAlignedBox_xyz(_position_and_color.pos(),_position_and_color.pos()) ; 00076 } 00077 00078 const Vector3& Segment::vertex(int i) const 00079 { 00080 return ( (i&1)==0 )?P1.pos():P2.pos() ; 00081 } 00082 00083 AxisAlignedBox_xyz Segment::bbox() const 00084 { 00085 AxisAlignedBox_xyz B(P1.pos()); 00086 B.include(P2.pos()) ; 00087 00088 return B ; 00089 } 00090 00091 const Feedback3DColor& Polygone::sommet3DColor(int i) const 00092 { 00093 return _vertices[i % nbVertices()] ; 00094 } 00095 00096 const Vector3& Polygone::vertex(int i) const 00097 { 00098 return _vertices[i % nbVertices()].pos() ; 00099 } 00100 00101 00102 Polygone::Polygone(const vector<Feedback3DColor>& fc) 00103 : _vertices(fc) 00104 { 00105 initNormal() ; 00106 00107 for(unsigned int i=0;i<fc.size();i++) 00108 _bbox.include(fc[i].pos()) ; 00109 } 00110 00111 AxisAlignedBox_xyz Polygone::bbox() const 00112 { 00113 return _bbox ; 00114 } 00115 00116 double Polygone::equation(const Vector3& v) const 00117 { 00118 return v * _normal - _c ; 00119 } 00120 00121 void Polygone::initNormal() 00122 { 00123 FLOAT anglemax = 0.0 ; 00124 Vector3 normalmax = Vector3(0.0,0.0,0.0) ; 00125 FLOAT v12norm = (vertex(1)-vertex(0)).norm() ; 00126 00127 for(unsigned int i=0;i<nbVertices();i++) 00128 { 00129 Vector3 v1(vertex(i)) ; 00130 Vector3 v2(vertex(i+1)); 00131 Vector3 v3(vertex(i+2)) ; 00132 00133 Vector3 normal_tmp((v3-v2)^(v1-v2)) ; 00134 00135 FLOAT v32norm = (v3-v2).norm() ; 00136 00137 if(normal_tmp.z() > 0) 00138 normal_tmp *= -1.0 ; 00139 00140 if((v32norm > 0.0)&&(v12norm > 0.0)) 00141 { 00142 double anglemaxtmp = normal_tmp.norm()/v32norm/v12norm ; 00143 00144 if(anglemaxtmp > anglemax) 00145 { 00146 anglemax = anglemaxtmp ; 00147 normalmax = normal_tmp ; 00148 } 00149 } 00150 00151 v12norm = v32norm ; 00152 00153 if(anglemax > FLAT_POLYGON_EPS) // slight optimization 00154 break ; 00155 } 00156 00157 if(normalmax.infNorm() != 0.0) 00158 _normal = NVector3(normalmax) ; 00159 00160 anglefactor = anglemax ; 00161 _c = _normal*vertex(0) ; 00162 } 00163 00164 std::ostream& vrender::operator<<(std::ostream& o,const Feedback3DColor& f) 00165 { 00166 o << "(" << f.pos() << ") + (" << f.red() << "," << f.green() << "," << f.blue() << "," << f.alpha() << ")" << endl ; 00167 return o ; 00168 } 00169 00170