gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
Warp.cpp
Go to the documentation of this file.
1 // Gmsh - Copyright (C) 1997-2022 C. Geuzaine, J.-F. Remacle
2 //
3 // See the LICENSE.txt file in the Gmsh root directory for license information.
4 // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
5 
6 #include "Warp.h"
7 #include "SmoothData.h"
8 #include "Numeric.h"
9 
11  {GMSH_FULLRC, "Factor", nullptr, 1.},
12  {GMSH_FULLRC, "TimeStep", nullptr, 0.},
13  {GMSH_FULLRC, "SmoothingAngle", nullptr, 180.},
14  {GMSH_FULLRC, "View", nullptr, -1.},
15  {GMSH_FULLRC, "OtherView", nullptr, -1.}};
16 
17 extern "C" {
19 }
20 
21 std::string GMSH_WarpPlugin::getHelp() const
22 {
23  return "Plugin(Warp) transforms the elements in the "
24  "view `View' by adding to their node coordinates "
25  "the vector field stored in the `TimeStep'-th time "
26  "step of the view `OtherView', scaled by `Factor'.\n\n"
27  "If `View' < 0, the plugin is run on the current view.\n\n"
28  "If `OtherView' < 0, the vector field is taken as the "
29  "field of surface normals multiplied by the `TimeStep' "
30  "value in `View'. (The smoothing of the surface "
31  "normals is controlled by the `SmoothingAngle' "
32  "parameter.)\n\n"
33  "Plugin(Warp) is executed in-place.";
34 }
35 
37 {
38  return sizeof(WarpOptions_Number) / sizeof(StringXNumber);
39 }
40 
42 {
43  return &WarpOptions_Number[iopt];
44 }
45 
47 {
48  double factor = WarpOptions_Number[0].def;
49  int TimeStep = (int)WarpOptions_Number[1].def;
50  double AngleTol = WarpOptions_Number[2].def;
51  int iView = (int)WarpOptions_Number[3].def;
52  int otherView = (int)WarpOptions_Number[4].def;
53 
54  PView *v1 = getView(iView, v);
55  if(!v1) return v;
56  if(otherView < 0) otherView = iView;
57  PView *v2 = getView(otherView, v);
58  if(!v2) return v;
59 
62 
63  // sanity checks
64  if(data1->getNumEntities() != data2->getNumEntities() ||
65  data1->getNumElements() != data2->getNumElements()) {
66  Msg::Error("Incompatible views");
67  return v;
68  }
69  if(TimeStep < 0 || TimeStep > data2->getNumTimeSteps() - 1) {
70  Msg::Error("Invalid TimeStep (%d) in View[%d]", TimeStep, v2->getIndex());
71  return v;
72  }
73 
74  // create smooth normal field if we don't have an explicit warp field
75  smooth_normals *normals = nullptr;
76  if(otherView < 0) {
77  normals = new smooth_normals(AngleTol);
78  for(int ent = 0; ent < data1->getNumEntities(0); ent++) {
79  for(int ele = 0; ele < data1->getNumElements(0, ent); ele++) {
80  if(data1->skipElement(0, ent, ele)) continue;
81  int numEdges = data1->getNumEdges(0, ent, ele);
82  if(numEdges == 3 || numEdges == 4) {
83  double x[4], y[4], z[4], n[4];
84  for(int nod = 0; nod < numEdges; nod++)
85  data1->getNode(0, ent, ele, nod, x[nod], y[nod], z[nod]);
86  normal3points(x[0], y[0], z[0], x[1], y[1], z[1], x[2], y[2], z[2],
87  n);
88  for(int nod = 0; nod < numEdges; nod++)
89  normals->add(x[nod], y[nod], z[nod], n[0], n[1], n[2]);
90  }
91  }
92  }
93  }
94 
95  if(data1->isNodeData()) {
96  // tag all the nodes with "0" (the default tag)
97  for(int step = 0; step < data1->getNumTimeSteps(); step++) {
98  for(int ent = 0; ent < data1->getNumEntities(step); ent++) {
99  for(int ele = 0; ele < data1->getNumElements(step, ent); ele++) {
100  if(data1->skipElement(step, ent, ele)) continue;
101  for(int nod = 0; nod < data1->getNumNodes(step, ent, ele); nod++)
102  data1->tagNode(step, ent, ele, nod, 0);
103  }
104  }
105  }
106  }
107 
108  // transform each "0" node: (x,y,z) += factor * mult * (valx, valy, valz)
109  for(int step = 0; step < data1->getNumTimeSteps(); step++) {
110  for(int ent = 0; ent < data1->getNumEntities(step); ent++) {
111  for(int ele = 0; ele < data1->getNumElements(step, ent); ele++) {
112  if(data1->skipElement(step, ent, ele)) continue;
113  int numNodes = data1->getNumNodes(step, ent, ele);
114  if(numNodes < 2) continue;
115  double x[8], y[8], z[8], n[3] = {0., 0., 0.};
116  int tag[8];
117  for(int nod = 0; nod < numNodes; nod++)
118  tag[nod] =
119  data1->getNode(step, ent, ele, nod, x[nod], y[nod], z[nod]);
120  int dim = data1->getDimension(step, ent, ele);
121  if(normals && dim == 2)
122  normal3points(x[0], y[0], z[0], x[1], y[1], z[1], x[2], y[2], z[2],
123  n);
124  for(int nod = 0; nod < numNodes; nod++) {
125  if(data1->isNodeData() && tag[nod]) continue; // already transformed
126  double mult = 1., val[3] = {n[0], n[1], n[2]};
127  if(normals) {
128  if(dim == 2) {
129  normals->get(x[nod], y[nod], z[nod], val[0], val[1], val[2]);
130  data1->getScalarValue(step, ent, ele, nod, mult);
131  }
132  }
133  else if(data2->getNumComponents(TimeStep, ent, ele) == 3 &&
134  data2->getNumNodes(TimeStep, ent, ele) == numNodes) {
135  for(int comp = 0; comp < 3; comp++)
136  data2->getValue(TimeStep, ent, ele, nod, comp, val[comp]);
137  }
138  x[nod] += factor * mult * val[0];
139  y[nod] += factor * mult * val[1];
140  z[nod] += factor * mult * val[2];
141  data1->setNode(step, ent, ele, nod, x[nod], y[nod], z[nod]);
142  if(data1->isNodeData()) data1->tagNode(step, ent, ele, nod, 1);
143  }
144  }
145  }
146  }
147 
148  if(normals) delete normals;
149 
150  data1->finalize();
151  v1->setChanged(true);
152 
153  return v1;
154 }
PViewData::getNumEdges
virtual int getNumEdges(int step, int ent, int ele)
Definition: PViewData.h:180
GMSH_WarpPlugin::getOption
StringXNumber * getOption(int iopt)
Definition: Warp.cpp:41
PView
Definition: PView.h:27
PViewData::skipElement
virtual bool skipElement(int step, int ent, int ele, bool checkVisibility=false, int samplingRate=1)
Definition: PViewData.cpp:90
WarpOptions_Number
StringXNumber WarpOptions_Number[]
Definition: Warp.cpp:10
GMSH_Plugin
Definition: Plugin.h:26
StringXNumber::def
double def
Definition: Options.h:922
PViewData::getNumTimeSteps
virtual int getNumTimeSteps()=0
Warp.h
GMSH_WarpPlugin::getNbOptions
int getNbOptions() const
Definition: Warp.cpp:36
PViewData::isNodeData
virtual bool isNodeData()
Definition: PViewData.h:218
PViewData::getNode
virtual int getNode(int step, int ent, int ele, int nod, double &x, double &y, double &z)
Definition: PViewData.h:141
Msg::Error
static void Error(const char *fmt,...)
Definition: GmshMessage.cpp:482
StringXNumber
Definition: Options.h:918
PViewData::getValue
virtual void getValue(int step, int ent, int ele, int idx, double &val)
Definition: PViewData.h:159
PView::getIndex
int getIndex()
Definition: PView.h:92
smooth_normals
Definition: SmoothData.h:100
GMSH_RegisterWarpPlugin
GMSH_Plugin * GMSH_RegisterWarpPlugin()
Definition: Warp.cpp:18
PViewData::setNode
virtual void setNode(int step, int ent, int ele, int nod, double x, double y, double z)
Definition: PViewData.cpp:124
PViewData::getNumEntities
virtual int getNumEntities(int step=-1)
Definition: PViewData.h:127
smooth_normals::add
void add(double x, double y, double z, double nx, double ny, double nz)
Definition: SmoothData.cpp:215
PView::setChanged
void setChanged(bool val)
Definition: PView.cpp:241
GMSH_WarpPlugin
Definition: Warp.h:15
PViewData::getDimension
virtual int getDimension(int step, int ent, int ele)
Definition: PViewData.h:134
PViewData::tagNode
virtual void tagNode(int step, int ent, int ele, int nod, int tag)
Definition: PViewData.h:148
PViewData::getScalarValue
void getScalarValue(int step, int ent, int ele, int nod, double &val, int tensorRep=0, int forceNumComponents=0, int componentMap[9]=nullptr)
Definition: PViewData.cpp:97
GMSH_FULLRC
#define GMSH_FULLRC
Definition: Options.h:20
GMSH_WarpPlugin::getHelp
std::string getHelp() const
Definition: Warp.cpp:21
Numeric.h
PViewData::getNumNodes
virtual int getNumNodes(int step, int ent, int ele)
Definition: PViewData.h:137
mult
Quaternion mult(const Quaternion &A, const Quaternion &B)
Definition: Camera.cpp:459
PViewData
Definition: PViewData.h:29
PViewData::getNumComponents
virtual int getNumComponents(int step, int ent, int ele)
Definition: PViewData.h:152
smooth_normals::get
bool get(double x, double y, double z, double &nx, double &ny, double &nz) const
Definition: SmoothData.cpp:234
z
const double z
Definition: GaussQuadratureQuad.cpp:56
GMSH_PostPlugin::getView
virtual PView * getView(int index, PView *view)
Definition: Plugin.cpp:81
GMSH_PostPlugin::getPossiblyAdaptiveData
virtual PViewData * getPossiblyAdaptiveData(PView *view)
Definition: Plugin.cpp:94
normal3points
void normal3points(double x0, double y0, double z0, double x1, double y1, double z1, double x2, double y2, double z2, double n[3])
Definition: Numeric.cpp:76
PViewData::getNumElements
virtual int getNumElements(int step=-1, int ent=-1)
Definition: PViewData.h:131
GMSH_WarpPlugin::execute
PView * execute(PView *)
Definition: Warp.cpp:46
PViewData::finalize
virtual bool finalize(bool computeMinMax=true, const std::string &interpolationScheme="")
Definition: PViewData.cpp:30
SmoothData.h