-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMeshVertexDisplacementOp.cs
164 lines (127 loc) · 5.36 KB
/
MeshVertexDisplacementOp.cs
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
// Copyright (c) Ryan Schmidt ([email protected]) - All Rights Reserved
// Unauthorized copying of this file, via any medium is strictly prohibited. Proprietary and confidential.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using g3;
namespace gs
{
public class MeshVertexDisplacementOp : BaseDMeshSourceOp
{
DMesh3 DisplacedMesh;
double heat_map_max_dist = 1.0;
public double HeatMapMaxDistance {
get { return heat_map_max_dist; }
set { heat_map_max_dist = value; base.invalidate(); }
}
bool enable_heat_map = true;
public bool EnableHeatMap {
get { return enable_heat_map; }
set { enable_heat_map = value; base.invalidate(); }
}
DMeshSourceOp mesh_source;
public DMeshSourceOp MeshSource {
get { return mesh_source; }
set {
if (mesh_source != null)
mesh_source.OperatorModified -= on_input_modified;
mesh_source = value;
if (mesh_source != null)
mesh_source.OperatorModified += on_input_modified;
base.invalidate();
}
}
IVectorDisplacementSourceOp displace_source;
public IVectorDisplacementSourceOp DisplacementSource {
get { return displace_source; }
set {
if (displace_source != null)
displace_source.OperatorModified -= on_input_modified;
displace_source = value;
if (displace_source != null)
displace_source.OperatorModified += on_input_modified;
base.invalidate();
}
}
protected virtual void on_input_modified(ModelingOperator op) {
base.invalidate();
}
public virtual void Update()
{
base.begin_update();
if (MeshSource == null)
throw new Exception("MeshVertexDisplacementOp: must set valid MeshSource to compute!");
if (DisplacementSource == null)
throw new Exception("MeshVertexDisplacementOp: must set valid DisplacementSource to compute!");
DMesh3 meshIn = MeshSource.GetDMeshUnsafe();
IVectorDisplacement displace = DisplacementSource.GetDisplacement();
if ( displace.Count != 0 && displace.Count != meshIn.MaxVertexID )
throw new Exception("MeshVertexDisplacementOp: inconsistent counts " + displace.Count.ToString() + " != " + meshIn.MaxVertexID.ToString());
DMesh3 mesh = new DMesh3(meshIn, MeshHints.None);
//if (!mesh.HasVertexNormals)
// MeshNormals.QuickCompute(mesh);
if (displace.Count > 0) {
gParallel.ForEach(mesh.VertexIndices(), (vid) => {
Vector3d dv = displace.GetDisplacementForIndex(vid);
//Vector3f n = mesh.GetVertexNormal(vid);
Vector3d v = mesh.GetVertex(vid);
v += dv;
mesh.SetVertex(vid, v);
});
if (enable_heat_map) {
// compute max displace len
ColorMap map = new ColorMap();
map.AddPoint(0, Colorf.CornflowerBlue);
float d = (float)HeatMapMaxDistance;
map.AddPoint(d, Colorf.Orange);
map.AddPoint(2 * d, Colorf.VideoYellow);
map.AddPoint(4 * d, Colorf.VideoRed);
map.AddPoint(-d, Colorf.VideoMagenta);
float max_displace = d;
gParallel.ForEach(mesh.VertexIndices(), (vid) => {
Vector3f dv = (Vector3f)displace.GetDisplacementForIndex(vid);
Vector3f n = mesh.GetVertexNormal(vid);
float sign = n.Dot(dv) > 0 ? 1 : -1;
Colorf c = map.Linear(dv.Length * sign);
Colorf existing_c = mesh.GetVertexColor(vid);
float preserve__max = max_displace / 2;
float t = MathUtil.Clamp(dv.Length / preserve__max, 0.0f, 1.0f);
c = (1.0f - t) * existing_c + (t) * c;
mesh.SetVertexColor(vid, c);
//float t = MathUtil.Clamp(dv.Length / max_displace, -1.0f, 1.0f);
//mesh.SetVertexColor(vid, t * Colorf.Orange);
});
}
}
MeshNormals.QuickCompute(mesh);
DisplacedMesh = mesh;
base.complete_update();
}
/*
* IMeshSourceOp / DMeshSourceOp interface
*/
public override IMesh GetIMesh() {
if (base.requires_update())
Update();
return DisplacedMesh;
}
public override DMesh3 GetDMeshUnsafe() {
return (DMesh3)GetIMesh();
}
public override bool HasSpatial {
get { return false; }
}
public override ISpatial GetSpatial() {
return null;
}
public override DMesh3 ExtractDMesh()
{
Update();
var result = DisplacedMesh;
DisplacedMesh = null;
base.result_consumed();
return result;
}
}
}