ROOT logo
// @(#)root/gl:$Id: TGLIsoMesh.h 33600 2010-05-21 09:24:32Z rdm $
// Author:  Timur Pocheptsov  06/01/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.             *
 *************************************************************************/

#ifndef ROOT_TGLIsoMesh
#define ROOT_TGLIsoMesh

#include <vector>

#ifndef ROOT_Rtypes
#include "Rtypes.h"
#endif
#ifndef ROOT_TAxis
#include "TAxis.h"
#endif

class TGLBoxCut;

namespace Rgl {
namespace Mc {

/*
TIsoMesh - set of vertices, per-vertex normals, "triangles". 
Each "triangle" is a triplet of indices, pointing into vertices 
and normals arrays. For example, triangle t = {1, 4, 6}
has vertices &fVerts[1 * 3], &fVerts[4 * 3], &fVerts[6 * 3];
and normals &fNorms[1 * 3], &fNorms[4 * 3], &fNorms[6 * 3]
"V" parameter should be Float_t or Double_t (or some
integral type?).

Prefix "T" in a class name only for code-style checker.
*/

template<class V>
class TIsoMesh {
public:
   UInt_t AddVertex(const V *v)
   {
      const UInt_t index = UInt_t(fVerts.size() / 3);
      fVerts.push_back(v[0]);
      fVerts.push_back(v[1]);
      fVerts.push_back(v[2]);

      return index;
   }

   void AddNormal(const V *n)
   {
      fNorms.push_back(n[0]);
      fNorms.push_back(n[1]);
      fNorms.push_back(n[2]);
   }

   UInt_t AddTriangle(const UInt_t *t)
   {
      const UInt_t index = UInt_t(fTris.size() / 3);
      fTris.push_back(t[0]);
      fTris.push_back(t[1]);
      fTris.push_back(t[2]);

      return index;
   }

   void Swap(TIsoMesh &rhs)
   {
      std::swap(fVerts, rhs.fVerts);
      std::swap(fNorms, rhs.fNorms);
      std::swap(fTris, rhs.fTris);
   }

   void ClearMesh()
   {
      fVerts.clear();
      fNorms.clear();
      fTris.clear();
   }

   std::vector<V>      fVerts;
   std::vector<V>      fNorms;
   std::vector<UInt_t> fTris;
};

/*
TGridGeometry describes ranges and cell steps (scales are 
already in steps and ranges).
*/
template<class V>
class TGridGeometry {
public:
   enum EVertexPosition{
      kBinCenter,
      kBinEdge
   };

   TGridGeometry() : fMinX(0),  fStepX(0),
                     fMinY(0),  fStepY(0),
                     fMinZ(0),  fStepZ(0),
                     fXScaleInverted(1.),
                     fYScaleInverted(1.),
                     fZScaleInverted(1.)
   {
      //Default constructor.
   }
   
   TGridGeometry(const TAxis *x, const TAxis *y, const TAxis *z,
                 Double_t xs = 1., Double_t ys = 1., Double_t zs = 1.,
                 EVertexPosition pos = kBinCenter)
         : fMinX(0),  fStepX(0),
           fMinY(0),  fStepY(0),
           fMinZ(0),  fStepZ(0),
           fXScaleInverted(1.),
           fYScaleInverted(1.),
           fZScaleInverted(1.)
   {
      //Define geometry using TAxis.
      if (pos == kBinCenter) {
         fMinX  = V(x->GetBinCenter(x->GetFirst()));
         fStepX = V((x->GetBinCenter(x->GetLast()) - fMinX) / (x->GetNbins() - 1));
         fMinY  = V(y->GetBinCenter(y->GetFirst()));
         fStepY = V((y->GetBinCenter(y->GetLast()) - fMinY) / (y->GetNbins() - 1));
         fMinZ  = V(z->GetBinCenter(z->GetFirst()));
         fStepZ = V((z->GetBinCenter(z->GetLast()) - fMinZ) / (z->GetNbins() - 1));

         fMinX *= xs, fStepX *= xs;
         fMinY *= ys, fStepY *= ys;
         fMinZ *= zs, fStepZ *= zs;
      } else if (pos == kBinEdge) {
         fMinX  = V(x->GetBinLowEdge(x->GetFirst()));
         fStepX = V((x->GetBinUpEdge(x->GetLast()) - fMinX) / (x->GetNbins()));
         fMinY  = V(y->GetBinLowEdge(y->GetFirst()));
         fStepY = V((y->GetBinUpEdge(y->GetLast()) - fMinY) / (y->GetNbins()));
         fMinZ  = V(z->GetBinLowEdge(z->GetFirst()));
         fStepZ = V((z->GetBinUpEdge(z->GetLast()) - fMinZ) / (z->GetNbins()));

         fMinX *= xs, fStepX *= xs;
         fMinY *= ys, fStepY *= ys;
         fMinZ *= zs, fStepZ *= zs;
      }

      fXScaleInverted = 1. / xs;
      fYScaleInverted = 1. / ys;
      fZScaleInverted = 1. / zs;
   }
   
   V fMinX;
   V fStepX;

   V fMinY;
   V fStepY;

   V fMinZ;
   V fStepZ;

   V fXScaleInverted;
   V fYScaleInverted;
   V fZScaleInverted;
};

}//namespace Mc

//Auxilary functions to draw an iso mesh in different modes.
void DrawMesh(const std::vector<Float_t> &vs, const std::vector<Float_t> &ns, 
              const std::vector<UInt_t> &ts);
void DrawMesh(const std::vector<Double_t> &vs, const std::vector<Double_t> &ns, 
              const std::vector<UInt_t> &ts);

void DrawMesh(const std::vector<Float_t> &vs, const std::vector<UInt_t> &fTS);
void DrawMesh(const std::vector<Double_t> &vs, const std::vector<UInt_t> &fTS);

void DrawMesh(const std::vector<Float_t> &vs, const std::vector<Float_t> &ns, 
              const std::vector<UInt_t> &ts, const TGLBoxCut &box);
void DrawMesh(const std::vector<Double_t> &vs, const std::vector<Double_t> &ns, 
              const std::vector<UInt_t> &ts, const TGLBoxCut &box);

void DrawMesh(const std::vector<Float_t> &vs, const std::vector<UInt_t> &ts,
              const TGLBoxCut &box);
void DrawMesh(const std::vector<Double_t> &vs, const std::vector<UInt_t> &ts,
              const TGLBoxCut &box);

void DrawMesh(const std::vector<Double_t> &vs, const std::vector<UInt_t> &ts, 
              const TGLBoxCut &box);
void DrawMesh(const std::vector<Float_t> &vs, const std::vector<UInt_t> &ts, 
              const TGLBoxCut &box);

void DrawMapleMesh(const std::vector<Double_t> &vs, const std::vector<Double_t> &ns,
                   const std::vector<UInt_t> &ts);
void DrawMapleMesh(const std::vector<Double_t> &vs, const std::vector<Double_t> &ns,
                   const std::vector<UInt_t> &ts, const TGLBoxCut & box);

}//namespace Rgl

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