ROOT logo
// @(#)root/gl:$Id$
// Author:  Timur Pocheptsov  28/07/2009

/*************************************************************************
 * Copyright (C) 1995-2009, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#include "TGLIncludes.h"

#include "TGLPlotPainter.h"
#include "TGLIsoMesh.h"

namespace Rgl {

//Functions for TGLTF3/TGLIso/TGL5DPainter.
//______________________________________________________________________________
template<class V>
void DrawMesh(GLenum type, const std::vector<V> &vs, const std::vector<V> &ns, 
              const std::vector<UInt_t> &fTS)
{
   //Surface with material and lighting.
   glEnableClientState(GL_VERTEX_ARRAY);
   glEnableClientState(GL_NORMAL_ARRAY);
   glVertexPointer(3, type, 0, &vs[0]);
   glNormalPointer(type, 0, &ns[0]);
   glDrawElements(GL_TRIANGLES, fTS.size(), GL_UNSIGNED_INT, &fTS[0]);
   glDisableClientState(GL_NORMAL_ARRAY);
   glDisableClientState(GL_VERTEX_ARRAY);
}

//______________________________________________________________________________
void DrawMesh(const std::vector<Float_t> &vs, const std::vector<Float_t> &ns, 
              const std::vector<UInt_t> &ts)
{
   //Call function-template.
   DrawMesh(GL_FLOAT, vs, ns, ts);
}

//______________________________________________________________________________
void DrawMesh(const std::vector<Double_t> &vs, const std::vector<Double_t> &ns, 
              const std::vector<UInt_t> &ts)
{
   //Call function-template.
   DrawMesh(GL_DOUBLE, vs, ns, ts);
}

//______________________________________________________________________________
template<class V>
void DrawMesh(GLenum type, const std::vector<V> &vs, const std::vector<UInt_t> &fTS)
{
   //Only vertices, no normal (no lighting and material).
   glEnableClientState(GL_VERTEX_ARRAY);
   glVertexPointer(3, type, 0, &vs[0]);
   glDrawElements(GL_TRIANGLES, fTS.size(), GL_UNSIGNED_INT, &fTS[0]);
   glDisableClientState(GL_VERTEX_ARRAY);
}

//______________________________________________________________________________
void DrawMesh(const std::vector<Float_t> &vs, const std::vector<UInt_t> &ts)
{
   //Call function-template.
   DrawMesh(GL_FLOAT, vs, ts);
}

//______________________________________________________________________________
void DrawMesh(const std::vector<Double_t> &vs, const std::vector<UInt_t> &ts)
{
   //Call function-template.
   DrawMesh(GL_DOUBLE, vs, ts);
}

//______________________________________________________________________________
template<class V, class GLN, class GLV>
void DrawMesh(GLN normal3, GLV vertex3, const std::vector<V> &vs, 
              const std::vector<V> &ns, const std::vector<UInt_t> &fTS, 
              const TGLBoxCut &box)
{
   //Mesh with cut.
   //Material and lighting are enabled.
   glBegin(GL_TRIANGLES);

   for (UInt_t i = 0, e = fTS.size() / 3; i < e; ++i) {
      const UInt_t * t = &fTS[i * 3];
      if (box.IsInCut(&vs[t[0] * 3]))
         continue;
      if (box.IsInCut(&vs[t[1] * 3]))
         continue;
      if (box.IsInCut(&vs[t[2] * 3]))
         continue;

      normal3(&ns[t[0] * 3]);
      vertex3(&vs[t[0] * 3]);
      
      normal3(&ns[t[1] * 3]);
      vertex3(&vs[t[1] * 3]);
      
      normal3(&ns[t[2] * 3]);
      vertex3(&vs[t[2] * 3]);
   }

   glEnd();
}

//______________________________________________________________________________
void DrawMesh(const std::vector<Float_t> &vs, const std::vector<Float_t> &ns, 
              const std::vector<UInt_t> &ts, const TGLBoxCut &box)
{
   //Call function-template.
   DrawMesh(&glNormal3fv, &glVertex3fv, vs,  ns, ts, box);
}

//______________________________________________________________________________
void DrawMesh(const std::vector<Double_t> &vs, const std::vector<Double_t> &ns, 
              const std::vector<UInt_t> &ts, const TGLBoxCut &box)
{
   //Call function-template.
   DrawMesh(&glNormal3dv, &glVertex3dv, vs, ns, ts, box);
}

//______________________________________________________________________________
template<class V, class GLV>
void DrawMesh(GLV vertex3, const std::vector<V> &vs, const std::vector<UInt_t> &fTS, 
              const TGLBoxCut &box)
{
   //Mesh with cut.
   //No material and lighting.
   glBegin(GL_TRIANGLES);

   for (UInt_t i = 0, e = fTS.size() / 3; i < e; ++i) {
      const UInt_t * t = &fTS[i * 3];
      if (box.IsInCut(&vs[t[0] * 3]))
         continue;
      if (box.IsInCut(&vs[t[1] * 3]))
         continue;
      if (box.IsInCut(&vs[t[2] * 3]))
         continue;

      vertex3(&vs[t[0] * 3]);
      vertex3(&vs[t[1] * 3]);
      vertex3(&vs[t[2] * 3]);
   }

   glEnd();
}

//______________________________________________________________________________
void DrawMesh(const std::vector<Float_t> &vs, const std::vector<UInt_t> &ts, const TGLBoxCut &box)
{
   //Call function-template.
   DrawMesh(&glVertex3fv, vs, ts, box);
}

//______________________________________________________________________________
void DrawMesh(const std::vector<Double_t> &vs, const std::vector<UInt_t> &ts, const TGLBoxCut &box)
{
   //Call function-template.
   DrawMesh(&glVertex3dv, vs, ts, box);
}

//______________________________________________________________________________
void NormalToColor(Double_t *rfColor, const Double_t *n)
{
   //NormalToColor generates a color from a given normal
   const Double_t x = n[0];
   const Double_t y = n[1];
   const Double_t z = n[2];
   rfColor[0] = (x > 0. ? x : 0.) + (y < 0. ? -0.5 * y : 0.) + (z < 0. ? -0.5 * z : 0.);
   rfColor[1] = (y > 0. ? y : 0.) + (z < 0. ? -0.5 * z : 0.) + (x < 0. ? -0.5 * x : 0.);
   rfColor[2] = (z > 0. ? z : 0.) + (x < 0. ? -0.5 * x : 0.) + (y < 0. ? -0.5 * y : 0.);
}

//______________________________________________________________________________
void DrawMapleMesh(const std::vector<Double_t> &vs, const std::vector<Double_t> &ns,
                   const std::vector<UInt_t> &fTS)
{
   //Colored mesh with lighting disabled.
   Double_t color[] = {0., 0., 0., 0.15};

   glBegin(GL_TRIANGLES);

   for (UInt_t i = 0, e = fTS.size() / 3; i < e; ++i) {
      const UInt_t *t = &fTS[i * 3];
      const Double_t * n = &ns[t[0] * 3];
      //
      NormalToColor(color, n);
      glColor4dv(color);
      glVertex3dv(&vs[t[0] * 3]);
      //
      n = &ns[t[1] * 3];
      NormalToColor(color, n);
      glColor4dv(color);
      glVertex3dv(&vs[t[1] * 3]);
      //
      n = &ns[t[2] * 3];
      NormalToColor(color, n);
      glColor4dv(color);
      glVertex3dv(&vs[t[2] * 3]);
   }

   glEnd();
}

//______________________________________________________________________________
void DrawMapleMesh(const std::vector<Double_t> &vs, const std::vector<Double_t> &ns,
                   const std::vector<UInt_t> &fTS, const TGLBoxCut & box)
{
   //Colored mesh with cut and disabled lighting.
   Double_t color[] = {0., 0., 0., 0.15};

   glBegin(GL_TRIANGLES);

   for (UInt_t i = 0, e = fTS.size() / 3; i < e; ++i) {
      const UInt_t *t = &fTS[i * 3];
      if (box.IsInCut(&vs[t[0] * 3]))
         continue;
      if (box.IsInCut(&vs[t[1] * 3]))
         continue;
      if (box.IsInCut(&vs[t[2] * 3]))
         continue;
      const Double_t * n = &ns[t[0] * 3];
      //
      NormalToColor(color, n);
      glColor4dv(color);
      glVertex3dv(&vs[t[0] * 3]);
      //
      n = &ns[t[1] * 3];
      NormalToColor(color, n);
      glColor4dv(color);
      glVertex3dv(&vs[t[1] * 3]);
      //
      n = &ns[t[2] * 3];
      NormalToColor(color, n);
      glColor4dv(color);
      glVertex3dv(&vs[t[2] * 3]);
   }

   glEnd();
}

}
 TGLIsoMesh.cxx:1
 TGLIsoMesh.cxx:2
 TGLIsoMesh.cxx:3
 TGLIsoMesh.cxx:4
 TGLIsoMesh.cxx:5
 TGLIsoMesh.cxx:6
 TGLIsoMesh.cxx:7
 TGLIsoMesh.cxx:8
 TGLIsoMesh.cxx:9
 TGLIsoMesh.cxx:10
 TGLIsoMesh.cxx:11
 TGLIsoMesh.cxx:12
 TGLIsoMesh.cxx:13
 TGLIsoMesh.cxx:14
 TGLIsoMesh.cxx:15
 TGLIsoMesh.cxx:16
 TGLIsoMesh.cxx:17
 TGLIsoMesh.cxx:18
 TGLIsoMesh.cxx:19
 TGLIsoMesh.cxx:20
 TGLIsoMesh.cxx:21
 TGLIsoMesh.cxx:22
 TGLIsoMesh.cxx:23
 TGLIsoMesh.cxx:24
 TGLIsoMesh.cxx:25
 TGLIsoMesh.cxx:26
 TGLIsoMesh.cxx:27
 TGLIsoMesh.cxx:28
 TGLIsoMesh.cxx:29
 TGLIsoMesh.cxx:30
 TGLIsoMesh.cxx:31
 TGLIsoMesh.cxx:32
 TGLIsoMesh.cxx:33
 TGLIsoMesh.cxx:34
 TGLIsoMesh.cxx:35
 TGLIsoMesh.cxx:36
 TGLIsoMesh.cxx:37
 TGLIsoMesh.cxx:38
 TGLIsoMesh.cxx:39
 TGLIsoMesh.cxx:40
 TGLIsoMesh.cxx:41
 TGLIsoMesh.cxx:42
 TGLIsoMesh.cxx:43
 TGLIsoMesh.cxx:44
 TGLIsoMesh.cxx:45
 TGLIsoMesh.cxx:46
 TGLIsoMesh.cxx:47
 TGLIsoMesh.cxx:48
 TGLIsoMesh.cxx:49
 TGLIsoMesh.cxx:50
 TGLIsoMesh.cxx:51
 TGLIsoMesh.cxx:52
 TGLIsoMesh.cxx:53
 TGLIsoMesh.cxx:54
 TGLIsoMesh.cxx:55
 TGLIsoMesh.cxx:56
 TGLIsoMesh.cxx:57
 TGLIsoMesh.cxx:58
 TGLIsoMesh.cxx:59
 TGLIsoMesh.cxx:60
 TGLIsoMesh.cxx:61
 TGLIsoMesh.cxx:62
 TGLIsoMesh.cxx:63
 TGLIsoMesh.cxx:64
 TGLIsoMesh.cxx:65
 TGLIsoMesh.cxx:66
 TGLIsoMesh.cxx:67
 TGLIsoMesh.cxx:68
 TGLIsoMesh.cxx:69
 TGLIsoMesh.cxx:70
 TGLIsoMesh.cxx:71
 TGLIsoMesh.cxx:72
 TGLIsoMesh.cxx:73
 TGLIsoMesh.cxx:74
 TGLIsoMesh.cxx:75
 TGLIsoMesh.cxx:76
 TGLIsoMesh.cxx:77
 TGLIsoMesh.cxx:78
 TGLIsoMesh.cxx:79
 TGLIsoMesh.cxx:80
 TGLIsoMesh.cxx:81
 TGLIsoMesh.cxx:82
 TGLIsoMesh.cxx:83
 TGLIsoMesh.cxx:84
 TGLIsoMesh.cxx:85
 TGLIsoMesh.cxx:86
 TGLIsoMesh.cxx:87
 TGLIsoMesh.cxx:88
 TGLIsoMesh.cxx:89
 TGLIsoMesh.cxx:90
 TGLIsoMesh.cxx:91
 TGLIsoMesh.cxx:92
 TGLIsoMesh.cxx:93
 TGLIsoMesh.cxx:94
 TGLIsoMesh.cxx:95
 TGLIsoMesh.cxx:96
 TGLIsoMesh.cxx:97
 TGLIsoMesh.cxx:98
 TGLIsoMesh.cxx:99
 TGLIsoMesh.cxx:100
 TGLIsoMesh.cxx:101
 TGLIsoMesh.cxx:102
 TGLIsoMesh.cxx:103
 TGLIsoMesh.cxx:104
 TGLIsoMesh.cxx:105
 TGLIsoMesh.cxx:106
 TGLIsoMesh.cxx:107
 TGLIsoMesh.cxx:108
 TGLIsoMesh.cxx:109
 TGLIsoMesh.cxx:110
 TGLIsoMesh.cxx:111
 TGLIsoMesh.cxx:112
 TGLIsoMesh.cxx:113
 TGLIsoMesh.cxx:114
 TGLIsoMesh.cxx:115
 TGLIsoMesh.cxx:116
 TGLIsoMesh.cxx:117
 TGLIsoMesh.cxx:118
 TGLIsoMesh.cxx:119
 TGLIsoMesh.cxx:120
 TGLIsoMesh.cxx:121
 TGLIsoMesh.cxx:122
 TGLIsoMesh.cxx:123
 TGLIsoMesh.cxx:124
 TGLIsoMesh.cxx:125
 TGLIsoMesh.cxx:126
 TGLIsoMesh.cxx:127
 TGLIsoMesh.cxx:128
 TGLIsoMesh.cxx:129
 TGLIsoMesh.cxx:130
 TGLIsoMesh.cxx:131
 TGLIsoMesh.cxx:132
 TGLIsoMesh.cxx:133
 TGLIsoMesh.cxx:134
 TGLIsoMesh.cxx:135
 TGLIsoMesh.cxx:136
 TGLIsoMesh.cxx:137
 TGLIsoMesh.cxx:138
 TGLIsoMesh.cxx:139
 TGLIsoMesh.cxx:140
 TGLIsoMesh.cxx:141
 TGLIsoMesh.cxx:142
 TGLIsoMesh.cxx:143
 TGLIsoMesh.cxx:144
 TGLIsoMesh.cxx:145
 TGLIsoMesh.cxx:146
 TGLIsoMesh.cxx:147
 TGLIsoMesh.cxx:148
 TGLIsoMesh.cxx:149
 TGLIsoMesh.cxx:150
 TGLIsoMesh.cxx:151
 TGLIsoMesh.cxx:152
 TGLIsoMesh.cxx:153
 TGLIsoMesh.cxx:154
 TGLIsoMesh.cxx:155
 TGLIsoMesh.cxx:156
 TGLIsoMesh.cxx:157
 TGLIsoMesh.cxx:158
 TGLIsoMesh.cxx:159
 TGLIsoMesh.cxx:160
 TGLIsoMesh.cxx:161
 TGLIsoMesh.cxx:162
 TGLIsoMesh.cxx:163
 TGLIsoMesh.cxx:164
 TGLIsoMesh.cxx:165
 TGLIsoMesh.cxx:166
 TGLIsoMesh.cxx:167
 TGLIsoMesh.cxx:168
 TGLIsoMesh.cxx:169
 TGLIsoMesh.cxx:170
 TGLIsoMesh.cxx:171
 TGLIsoMesh.cxx:172
 TGLIsoMesh.cxx:173
 TGLIsoMesh.cxx:174
 TGLIsoMesh.cxx:175
 TGLIsoMesh.cxx:176
 TGLIsoMesh.cxx:177
 TGLIsoMesh.cxx:178
 TGLIsoMesh.cxx:179
 TGLIsoMesh.cxx:180
 TGLIsoMesh.cxx:181
 TGLIsoMesh.cxx:182
 TGLIsoMesh.cxx:183
 TGLIsoMesh.cxx:184
 TGLIsoMesh.cxx:185
 TGLIsoMesh.cxx:186
 TGLIsoMesh.cxx:187
 TGLIsoMesh.cxx:188
 TGLIsoMesh.cxx:189
 TGLIsoMesh.cxx:190
 TGLIsoMesh.cxx:191
 TGLIsoMesh.cxx:192
 TGLIsoMesh.cxx:193
 TGLIsoMesh.cxx:194
 TGLIsoMesh.cxx:195
 TGLIsoMesh.cxx:196
 TGLIsoMesh.cxx:197
 TGLIsoMesh.cxx:198
 TGLIsoMesh.cxx:199
 TGLIsoMesh.cxx:200
 TGLIsoMesh.cxx:201
 TGLIsoMesh.cxx:202
 TGLIsoMesh.cxx:203
 TGLIsoMesh.cxx:204
 TGLIsoMesh.cxx:205
 TGLIsoMesh.cxx:206
 TGLIsoMesh.cxx:207
 TGLIsoMesh.cxx:208
 TGLIsoMesh.cxx:209
 TGLIsoMesh.cxx:210
 TGLIsoMesh.cxx:211
 TGLIsoMesh.cxx:212
 TGLIsoMesh.cxx:213
 TGLIsoMesh.cxx:214
 TGLIsoMesh.cxx:215
 TGLIsoMesh.cxx:216
 TGLIsoMesh.cxx:217
 TGLIsoMesh.cxx:218
 TGLIsoMesh.cxx:219
 TGLIsoMesh.cxx:220
 TGLIsoMesh.cxx:221
 TGLIsoMesh.cxx:222
 TGLIsoMesh.cxx:223
 TGLIsoMesh.cxx:224
 TGLIsoMesh.cxx:225
 TGLIsoMesh.cxx:226
 TGLIsoMesh.cxx:227
 TGLIsoMesh.cxx:228
 TGLIsoMesh.cxx:229
 TGLIsoMesh.cxx:230
 TGLIsoMesh.cxx:231
 TGLIsoMesh.cxx:232
 TGLIsoMesh.cxx:233
 TGLIsoMesh.cxx:234
 TGLIsoMesh.cxx:235
 TGLIsoMesh.cxx:236
 TGLIsoMesh.cxx:237
 TGLIsoMesh.cxx:238
 TGLIsoMesh.cxx:239
 TGLIsoMesh.cxx:240
 TGLIsoMesh.cxx:241
 TGLIsoMesh.cxx:242
 TGLIsoMesh.cxx:243
 TGLIsoMesh.cxx:244