1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
#ifndef GL_FIELD
#define GL_FIELD
#include <wrap/gl/space.h>
#include <wrap/gl/math.h>
#include <vcg/complex/algorithms/parametrization/tangent_field_operators.h>
#include <vcg/complex/allocate.h>
namespace vcg{
template <class MeshType>
class GLField
{
typedef typename MeshType::FaceType FaceType;
typedef typename MeshType::VertexType VertexType;
typedef typename MeshType::CoordType CoordType;
typedef typename MeshType::ScalarType ScalarType;
public:
static void GLDrawField(CoordType dir[4],
const CoordType ¢er,
const ScalarType &size,
const ScalarType &Width0,
const ScalarType &Width1,
const vcg::Color4b &Color0,
const vcg::Color4b &Color1,
bool oneside,
bool onlyPD1)
{
CoordType dirN[4];
for (size_t i=0;i<4;i++)
{
dirN[i]=dir[i];
dirN[i].Normalize();
}
ScalarType size1=size;
if (oneside)size1=0;
glLineWidth(Width0);
vcg::glColor(Color0);
glBegin(GL_LINES);
glVertex(center+dirN[0]*size);
glVertex(center+dirN[2]*size1);
glEnd();
if (onlyPD1)return;
glLineWidth(Width1);
vcg::glColor(Color1);
glBegin(GL_LINES);
glVertex(center+dirN[1]*size);
glVertex(center+dirN[3]*size1);
glEnd();
}
// ///draw the cross field of a given face in a given position
// static void GLDrawSingleFaceField(const FaceType &f,
// CoordType pos,
// ScalarType &size,
// bool onlyPD1,
// bool oneside)
// {
// CoordType center=pos;
// CoordType normal=f.cN();
// CoordType dir[4];
// vcg::tri::CrossField<MeshType>::CrossVector(f,dir);
// GLDrawField(dir,center,size,onlyPD1,oneside);
// }
///draw the cross field of a given face
static void GLDrawSingleFaceField(const FaceType &f,
const ScalarType &size,
const bool oneside,
const bool onlyPD1,
const ScalarType maxN,
const ScalarType minN,
const bool UseK)
{
assert(maxN>=minN);
CoordType center=(f.cP(0)+f.cP(1)+f.cP(2))/3;
//CoordType normal=f.cN();
CoordType dir[4];
vcg::tri::CrossField<MeshType>::CrossVector(f,dir);
if (maxN<=0)
GLDrawField(dir,center,size,2,2,vcg::Color4b(0,0,0,255),vcg::Color4b(0,0,0,255),oneside,onlyPD1);
else
{
ScalarType Norm0=dir[0].Norm();
ScalarType Norm1=dir[1].Norm();
if (UseK)
{
Norm0=f.cK1();
Norm1=f.cK2();
}
ScalarType MaxW=6;
ScalarType MinW=0.5;
ScalarType IntervW=MaxW-MinW;
if (Norm0>maxN)Norm0=maxN;
if (Norm1>maxN)Norm1=maxN;
if (Norm0<minN)Norm0=minN;
if (Norm1<minN)Norm1=minN;
vcg::Color4b Col0,Col1;
ScalarType W0,W1;
if (!UseK)
{
Col0=vcg::Color4b::ColorRamp(minN,maxN,Norm0);
Col1=vcg::Color4b::ColorRamp(minN,maxN,Norm1);
W0=(Norm0/(maxN-minN))*IntervW+MinW;
W1=(Norm1/(maxN-minN))*IntervW+MinW;
}
else
{
ScalarType MaxAbs=std::max(fabs(minN),fabs(maxN));
Col0=vcg::Color4b::ColorRamp(-MaxAbs,MaxAbs,Norm0);
Col1=vcg::Color4b::ColorRamp(-MaxAbs,MaxAbs,Norm1);
// if (Norm0<0)
// {
// assert(minN<0);
// //PUT green on ZERO
// Col0=vcg::Color4b::ColorRamp(minN,fabs(minN),Norm0);
// }else
// {
// //PUT green on ZERO
// Col0=vcg::Color4b::ColorRamp(-maxN,maxN,Norm0);
// }
// if (Norm1<0)
// {
// assert(minN<0);
// //PUT green on ZERO
// Col1=vcg::Color4b::ColorRamp(minN,fabs(minN),Norm1);
// }else
// {
// //PUT green on ZERO
// Col1=vcg::Color4b::ColorRamp(-maxN,maxN,Norm1);
// }
W0=(fabs(Norm0)/std::max(fabs(maxN),fabs(minN)))*IntervW+MinW;
W1=(fabs(Norm1)/std::max(fabs(maxN),fabs(minN)))*IntervW+MinW;
}
GLDrawField(dir,center,size,W0,W1,Col0,Col1,oneside,onlyPD1);
}
}
// static void GLDrawFaceSeams(const FaceType &f,
// vcg::Point3<bool> seams,
// vcg::Color4b seamCol[3])
// {
// glLineWidth(2);
// glBegin(GL_LINES);
// for (int i=0;i<3;i++)
// {
// if (!seams[i])continue;
// vcg::glColor(seamCol[i]);
// glVertex(f.V0(i)->P());
// glVertex(f.V1(i)->P());
// }
// glEnd();
// }
static void GLDrawVertField(const VertexType &v,
ScalarType &size)
{
CoordType center=v.cP();
CoordType normal=v.cN();
CoordType dir[4];
vcg::tri::CrossField<MeshType>::CrossVector(v,dir);
GLDrawField(dir,center,size,2,2,vcg::Color4b(0,0,0,255),vcg::Color4b(0,0,0,255),false,false);
}
static void GLDrawFaceField(const MeshType &mesh,
bool onlyPD1,
bool oneside,
ScalarType GlobalScale=0.002,
const ScalarType maxN=0,
const ScalarType minN=0,
bool UseK=false)
{
glPushAttrib(GL_ALL_ATTRIB_BITS);
glDepthRange(0.0,0.999);
glEnable(GL_COLOR_MATERIAL);
glDisable(GL_LIGHTING);
glDisable(GL_BLEND);
ScalarType size=mesh.bbox.Diag()*GlobalScale;
for (unsigned int i=0;i<mesh.face.size();i++)
{
if (mesh.face[i].IsD())continue;
GLDrawSingleFaceField(mesh.face[i],size,oneside,onlyPD1,maxN,minN,UseK);
}
glPopAttrib();
}
static void GLDrawVertField(const MeshType &mesh,ScalarType sizeF=0.01)
{
glPushAttrib(GL_ALL_ATTRIB_BITS);
glDepthRange(0.0,0.9999);
glEnable(GL_COLOR_MATERIAL);
glDisable(GL_LIGHTING);
glDisable(GL_BLEND);
ScalarType size=mesh.bbox.Diag()*sizeF;
for (int i=0;i<mesh.vert.size();i++)
{
if (mesh.vert[i].IsD())continue;
GLDrawVertField(mesh.vert[i],size);
}
glPopAttrib();
}
static void GLDrawSingularity(MeshType &mesh)
{
// query if an attribute is present or not
bool hasSingular = vcg::tri::HasPerVertexAttribute(mesh,std::string("Singular"));
bool hasSingularIndex = vcg::tri::HasPerVertexAttribute(mesh,std::string("SingularIndex"));
if (!hasSingular)return;
if(!hasSingularIndex)return;
typename MeshType::template PerVertexAttributeHandle<bool> Handle_Singular;
Handle_Singular=vcg::tri::Allocator<MeshType>::template GetPerVertexAttribute<bool>(mesh,std::string("Singular"));
typename MeshType::template PerVertexAttributeHandle<int> Handle_SingularIndex;
Handle_SingularIndex =vcg::tri::Allocator<MeshType>::template GetPerVertexAttribute<int>(mesh,std::string("SingularIndex"));
glPushAttrib(GL_ALL_ATTRIB_BITS);
glDepthRange(0.0,0.9999);
glEnable(GL_COLOR_MATERIAL);
glDisable(GL_LIGHTING);
glDisable(GL_BLEND);
glPointSize(20);
glBegin(GL_POINTS);
for (size_t i=0;i<mesh.vert.size();i++)
{
if (mesh.vert[i].IsD())continue;
if (!Handle_Singular[i])continue;
int SingIndex=Handle_SingularIndex[i];
vcg::Color4b colSing;
switch (SingIndex)
{
case 1:colSing=vcg::Color4b(0,0,255,255); break;
case 2:colSing=vcg::Color4b(0,255,0,255); break;
case 3:colSing=vcg::Color4b(255,0,0,255); break;
case 4:colSing=vcg::Color4b(255,255,0,255); break;
default:colSing=vcg::Color4b(255,0,255,255);
}
vcg::glColor(colSing);
vcg::glVertex(mesh.vert[i].P());
}
glEnd();
glPopAttrib();
}
};
}
#endif
|