This repository has been archived by the owner on Apr 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
slider_deform.h
215 lines (208 loc) · 7.08 KB
/
slider_deform.h
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
/*
* slider_deform.h
* sensitive couture
*
* Created by Nobuyuki Umetani on 12/20/10.
* Copyright 2010 The University of Tokyo and Columbia University. All rights reserved.
*
*/
#if !defined(SLIDER_DEFORM_H)
#define SLIDER_DEFORM_H
#include <iostream>
#include <vector>
#include "delfem/cad_obj2d_move.h"
class CSliderDeform
{
public:
void Clear(){
aSlider.clear();
aLoopDeform.clear();
}
unsigned int AddSlider(const std::string& str, double val, double min, double max) // register slider
{
unsigned int isl = aSlider.size();
aSlider.push_back( CSlider(str,val,min,max) );
return isl;
}
unsigned int count() const { return aSlider.size(); }
void AddSliderParamToLoop(unsigned int id_l, unsigned int islider,
unsigned int ixy, unsigned int idir) // set loop which slider it belongs and slider's direction
{
aLoopDeform.push_back( CLoopDeform(id_l,islider,ixy,idir) );
}
void ChangeSlider(double val_pos, double val_pre); // deform the loop with parameter change
void GetLamVtx(unsigned int islider, Cad::CCadObj2D_Move& cad_2d, std::vector<double>& aLamXY_Vtx) // set loop the parameter
{
{
unsigned int max_id_v = 0;
const std::vector<unsigned int>& aIdV = cad_2d.GetAryElemID(Cad::VERTEX);
for(unsigned int iiv=0;iiv<aIdV.size();iiv++){
max_id_v = ( aIdV[iiv] > max_id_v ) ? aIdV[iiv] : max_id_v;
}
aLamXY_Vtx.clear();
aLamXY_Vtx.resize((max_id_v+1)*4,0);
}
if( islider >= aSlider.size() ) return;
double val = aSlider[islider].GetValue();
for(unsigned int ild=0;ild<aLoopDeform.size();ild++){
aLoopDeform[ild].SetLambda(islider,val,cad_2d,aLamXY_Vtx);
}
}
void Draw(); // draw FFD structure
double GetValueSlider(unsigned int islider, double& min, double& max) const {
if( islider < aSlider.size() ){
aSlider[islider].GetMinMax(min,max);
return aSlider[islider].GetValue();
}
return 0;
}
void GetSliderProperty(unsigned int islider, std::string& name){
if( islider < aSlider.size() ){
name = aSlider[islider].name;
return;
}
return;
}
void SetLoopCenter(unsigned int id_l, double cnt_x, double cnt_y){
for(unsigned int ild=0;ild<aLoopDeform.size();ild++){
if( id_l == aLoopDeform[ild].id_l ){
aLoopDeform[ild].cnt_x = cnt_x;
aLoopDeform[ild].cnt_y = cnt_y;
}
}
}
void SetValueSlider(unsigned int islider, double val0){
if( islider >= aSlider.size() ) return;
aSlider[islider].SetValue(val0);
}
bool MoveCad(unsigned int islider, double val_pos, double val_pre,
Cad::CCadObj2D_Move& cad_2d)
{
bool res = false;
for(unsigned int ild=0;ild<aLoopDeform.size();ild++){
bool res0 = aLoopDeform[ild].MoveCad2(islider,val_pos,val_pre,cad_2d);
if( res0 ){ res = true; }
}
return res;
}
private:
class CSlider{
public:
CSlider(const std::string& str, double val, double min, double max){
name = str;
this->val = val;
this->min = min;
this->max = max;
}
double GetValue() const { return val; }
void SetValue(double val){
if( val < min ){ val = min; }
else if( val > max ){ val = max; }
this->val = val;
}
void GetMinMax(double& min, double& max) const {
min = this->min;
max = this->max;
}
public:
std::string name;
private:
double min;
double max;
double val;
};
class CLoopDeform{
public:
CLoopDeform(unsigned int id_l, unsigned int islider, unsigned int ixy, unsigned int idir){
this->id_l = id_l;
this->islider = islider;
this->ixy = ixy;
this->idir = idir;
this->cnt_x = 0;
this->cnt_y = 0;
}
bool MoveCad2(unsigned int islider, double val_pos, double val_pre,
Cad::CCadObj2D_Move& cad_2d)
{
if( this->islider != islider ) return false;
if( !cad_2d.IsElemID(Cad::LOOP,id_l) ) return false;
std::vector< std::pair<unsigned int, Com::CVector2D> > aIdDist;
for(Cad::CBRepSurface::CItrLoop itrl=cad_2d.GetItrLoop(id_l);!itrl.IsEnd();itrl.ShiftChildLoop()){
for(itrl.Begin();!itrl.IsEnd();itrl++){
unsigned int id_v = itrl.GetIdVertex();
Com::CVector2D vec0 = cad_2d.GetVertexCoord(id_v);
vec0.x -= cnt_x;
vec0.y -= cnt_y;
// std::cout << id_v << " " << ixy << " " << idir << " " << vec0.x << " " << vec0.y << std::endl;
if( ixy == 0 ){
if( (idir == 0 && vec0.x > 0)
|| (idir == 1 && vec0.x < 0)
|| (idir == 2 && vec0.y > 0)
|| (idir == 3 && vec0.y < 0)
|| idir == 4 ){
double x1 = vec0.x*(val_pos+1.0)/(val_pre+1.0);
aIdDist.push_back( std::make_pair(id_v,Com::CVector2D(x1+cnt_x,vec0.y+cnt_y)) );
}
}
else if( ixy == 1 ){
if( (idir == 0 && vec0.x > 0)
|| (idir == 1 && vec0.x < 0)
|| (idir == 2 && vec0.y > 0)
|| (idir == 3 && vec0.y < 0)
|| idir == 4 ){
double y1 = vec0.y*(val_pos+1.0)/(val_pre+1.0);
aIdDist.push_back( std::make_pair(id_v,Com::CVector2D(vec0.x+cnt_x,y1+cnt_y)) );
}
}
}
}
return cad_2d.MoveVertex(aIdDist);
}
void SetLambda(unsigned int islider, double val,
const Cad::CCadObj2D_Move& cad_2d,
std::vector<double>& aLamXY_Vtx)
{
if( this->islider != islider ) return;
if( !cad_2d.IsElemID(Cad::LOOP,id_l) ) return;
std::vector< std::pair<unsigned int, Com::CVector2D> > aIdDist;
for(Cad::CBRepSurface::CItrLoop itrl=cad_2d.GetItrLoop(id_l);!itrl.IsEnd();itrl.ShiftChildLoop()){
for(itrl.Begin();!itrl.IsEnd();itrl++){
unsigned int id_v = itrl.GetIdVertex();
Com::CVector2D vec0 = cad_2d.GetVertexCoord(id_v);
vec0.x -= cnt_x;
vec0.y -= cnt_y;
if( ixy == 0 ){
if( (idir == 0 && vec0.x > 0)
|| (idir == 1 && vec0.x < 0)
|| (idir == 2 && vec0.y > 0)
|| (idir == 3 && vec0.y < 0)
|| idir == 4 ){
double s0 = vec0.x/(val+1.0);
aLamXY_Vtx[id_v*4+0] = s0;
}
}
else if( ixy == 1 ){
if( (idir == 0 && vec0.x > 0)
|| (idir == 1 && vec0.x < 0)
|| (idir == 2 && vec0.y > 0)
|| (idir == 3 && vec0.y < 0)
|| idir == 4 ){
double s0 = vec0.y/(val+1.0);
aLamXY_Vtx[id_v*4+2] = s0;
}
}
}
}
}
public:
double cnt_x, cnt_y;
unsigned int id_l;
unsigned int islider;
unsigned int ixy; // 0:x, 0:y
unsigned int idir; // 0:yposi 1:ynega 2:xposi 3:xnega 4:all
};
private:
std::vector<CSlider> aSlider;
std::vector<CLoopDeform> aLoopDeform;
};
#endif