JavaCAD
MeshContainer.java
Go to the documentation of this file.
1 /*
2  * To change this license header, choose License Headers in Project Properties.
3  * To change this template file, choose Tools | Templates
4  * and open the template in the editor.
5  */
6 package eu.mihosoft.vrl.v3d;
7 
8 import java.util.ArrayList;
9 import java.util.Arrays;
10 import java.util.List;
11 import javafx.beans.value.ObservableValue;
12 import javafx.scene.Group;
13 import javafx.scene.PerspectiveCamera;
14 import javafx.scene.SceneAntialiasing;
15 import javafx.scene.SubScene;
16 import javafx.scene.input.MouseButton;
17 import javafx.scene.layout.Pane;
18 import javafx.scene.paint.Color;
19 import javafx.scene.paint.Material;
20 import javafx.scene.paint.PhongMaterial;
21 import javafx.scene.shape.CullFace;
22 import javafx.scene.shape.Mesh;
23 import javafx.scene.shape.MeshView;
24 
25 // TODO: Auto-generated Javadoc
31 public class MeshContainer {
32 
34  private final List<Mesh> meshes;
35 
37  private final List<Material> materials;
38 
40  private final double width;
41 
43  private final double height;
44 
46  private final double depth;
47 
49  private final Bounds bounds;
50 
52  private final Group root = new Group();
53 
55  private Pane viewContainer;
56 
58  private SubScene subScene;
59 
67  MeshContainer(Vector3d min, Vector3d max, Mesh... meshes) {
68  this(min, max, Arrays.asList(meshes));
69  }
70 
78  MeshContainer(Vector3d min, Vector3d max, List<Mesh> meshes) {
79  this.meshes = meshes;
80  this.materials = new ArrayList<>();
81  this.bounds = new Bounds(min, max);
82  this.width = bounds.getBounds().x;
83  this.height = bounds.getBounds().y;
84  this.depth = bounds.getBounds().z;
85 
86  PhongMaterial material = new PhongMaterial(Color.RED);
87  for (Mesh mesh : meshes) {
88  materials.add(material);
89  }
90  }
91 
100  MeshContainer(Vector3d min, Vector3d max, List<Mesh> meshes, List<Material> materials) {
101  this.meshes = meshes;
102  this.materials = materials;
103  this.bounds = new Bounds(min, max);
104  this.width = bounds.getBounds().x;
105  this.height = bounds.getBounds().y;
106  this.depth = bounds.getBounds().z;
107 
108  if (materials.size() != meshes.size()) {
109  throw new IllegalArgumentException("Mesh list and Material list must not differ in size!");
110  }
111 
112  }
113 
119  public double getWidth() {
120  return width;
121  }
122 
128  public double getHeight() {
129  return height;
130  }
131 
137  public double getDepth() {
138  return depth;
139  }
140 
146  public List<Mesh> getMeshes() {
147  return meshes;
148  }
149 
150  /* (non-Javadoc)
151  * @see java.lang.Object#toString()
152  */
153  @Override
154  public String toString() {
155  return bounds.toString();
156  }
157 
163  public Bounds getBounds() {
164  return bounds;
165  }
166 
172  public List<Material> getMaterials() {
173  return materials;
174  }
175 
181  public List<MeshView> getAsMeshViews() {
182  List<MeshView> result = new ArrayList<>(meshes.size());
183 
184  for (int i = 0; i < meshes.size(); i++) {
185 
186  Mesh mesh = meshes.get(i);
187  Material mat = materials.get(i);
188 
189  MeshView view = new MeshView(mesh);
190  view.setMaterial(mat);
191  view.setCullFace(CullFace.NONE);
192 
193  result.add(view);
194  }
195 
196  return result;
197  }
198 
199 // public javafx.scene.Node getAsInteractiveSubSceneNode() {
200 //
201 // if (viewContainer != null) {
202 // return viewContainer;
203 // }
204 //
205 // viewContainer = new Pane();
206 //
207 // SubScene subScene = new SubScene(getRoot(), 100, 100, true, SceneAntialiasing.BALANCED);
209 //
210 // subScene.widthProperty().bind(viewContainer.widthProperty());
211 // subScene.heightProperty().bind(viewContainer.heightProperty());
212 //
213 // PerspectiveCamera subSceneCamera = new PerspectiveCamera(false);
214 // subScene.setCamera(subSceneCamera);
215 //
216 // viewContainer.getChildren().add(subScene);
217 //
218 // getRoot().layoutXProperty().bind(viewContainer.widthProperty().divide(2));
219 // getRoot().layoutYProperty().bind(viewContainer.heightProperty().divide(2));
220 //
221 // viewContainer.boundsInLocalProperty().addListener(
222 // (ObservableValue<? extends javafx.geometry.Bounds> ov, javafx.geometry.Bounds t, javafx.geometry.Bounds t1) -> {
223 // setMeshScale(this, t1, getRoot());
224 // });
225 //
226 // VFX3DUtil.addMouseBehavior(getRoot(), viewContainer, MouseButton.PRIMARY);
227 //
228 // return viewContainer;
229 // }
230 //
231 // private void setMeshScale(MeshContainer meshContainer, javafx.geometry.Bounds t1, final Group meshView) {
232 // double maxDim
233 // = Math.max(meshContainer.getWidth(),
234 // Math.max(meshContainer.getHeight(), meshContainer.getDepth()));
235 //
236 // double minContDim = Math.min(t1.getWidth(), t1.getHeight());
237 //
238 // double scale = minContDim / (maxDim * 2);
239 //
240 // //System.out.println("scale: " + scale + ", maxDim: " + maxDim + ", " + meshContainer);
241 // meshView.setScaleX(scale);
242 // meshView.setScaleY(scale);
243 // meshView.setScaleZ(scale);
244 // }
245 }
final List< Material > materials
static Vector3d y(double y)
Definition: Vector3d.java:484
static Vector3d z(double z)
Definition: Vector3d.java:494
static Vector3d x(double x)
Definition: Vector3d.java:474