]> gerrit.simantics Code Review - simantics/3d.git/commitdiff
OcTree implementation 20/4420/1
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Wed, 2 Sep 2020 11:18:41 +0000 (14:18 +0300)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Wed, 2 Sep 2020 11:18:41 +0000 (14:18 +0300)
Created a separate plugin, so that 3D data structures remain independent
of G3D framework.

gitlab #140

Change-Id: Ia40bb149baff2c8f887a2c1be2f515679393a6c7

org.simantics.g3d.datastructures/.classpath [new file with mode: 0644]
org.simantics.g3d.datastructures/.gitignore [new file with mode: 0644]
org.simantics.g3d.datastructures/.project [new file with mode: 0644]
org.simantics.g3d.datastructures/.settings/org.eclipse.jdt.core.prefs [new file with mode: 0644]
org.simantics.g3d.datastructures/META-INF/MANIFEST.MF [new file with mode: 0644]
org.simantics.g3d.datastructures/build.properties [new file with mode: 0644]
org.simantics.g3d.datastructures/pom.xml [new file with mode: 0644]
org.simantics.g3d.datastructures/src/org/simantics/g3d/datastructures/Box.java [new file with mode: 0644]
org.simantics.g3d.datastructures/src/org/simantics/g3d/datastructures/OcTree.java [new file with mode: 0644]
org.simantics.g3d.feature/feature.xml
pom.xml

diff --git a/org.simantics.g3d.datastructures/.classpath b/org.simantics.g3d.datastructures/.classpath
new file mode 100644 (file)
index 0000000..eca7bdb
--- /dev/null
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+       <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
+       <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+       <classpathentry kind="src" path="src"/>
+       <classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/org.simantics.g3d.datastructures/.gitignore b/org.simantics.g3d.datastructures/.gitignore
new file mode 100644 (file)
index 0000000..ae3c172
--- /dev/null
@@ -0,0 +1 @@
+/bin/
diff --git a/org.simantics.g3d.datastructures/.project b/org.simantics.g3d.datastructures/.project
new file mode 100644 (file)
index 0000000..dce8db1
--- /dev/null
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+       <name>org.simantics.g3d.datastructures</name>
+       <comment></comment>
+       <projects>
+       </projects>
+       <buildSpec>
+               <buildCommand>
+                       <name>org.eclipse.jdt.core.javabuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+               <buildCommand>
+                       <name>org.eclipse.pde.ManifestBuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+               <buildCommand>
+                       <name>org.eclipse.pde.SchemaBuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+       </buildSpec>
+       <natures>
+               <nature>org.eclipse.pde.PluginNature</nature>
+               <nature>org.eclipse.jdt.core.javanature</nature>
+       </natures>
+</projectDescription>
diff --git a/org.simantics.g3d.datastructures/.settings/org.eclipse.jdt.core.prefs b/org.simantics.g3d.datastructures/.settings/org.eclipse.jdt.core.prefs
new file mode 100644 (file)
index 0000000..0c68a61
--- /dev/null
@@ -0,0 +1,7 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
+org.eclipse.jdt.core.compiler.compliance=1.8
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.8
diff --git a/org.simantics.g3d.datastructures/META-INF/MANIFEST.MF b/org.simantics.g3d.datastructures/META-INF/MANIFEST.MF
new file mode 100644 (file)
index 0000000..07e18e5
--- /dev/null
@@ -0,0 +1,10 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: G3D Datastructures
+Bundle-SymbolicName: org.simantics.g3d.datastructures
+Bundle-Version: 1.0.0.qualifier
+Bundle-Vendor: Semantum
+Automatic-Module-Name: org.simantics.g3d.datastructures
+Bundle-RequiredExecutionEnvironment: JavaSE-1.8
+Require-Bundle: javax.vecmath;bundle-version="1.5.2"
+Export-Package: org.simantics.g3d.datastructures
diff --git a/org.simantics.g3d.datastructures/build.properties b/org.simantics.g3d.datastructures/build.properties
new file mode 100644 (file)
index 0000000..34d2e4d
--- /dev/null
@@ -0,0 +1,4 @@
+source.. = src/
+output.. = bin/
+bin.includes = META-INF/,\
+               .
diff --git a/org.simantics.g3d.datastructures/pom.xml b/org.simantics.g3d.datastructures/pom.xml
new file mode 100644 (file)
index 0000000..7e06a4d
--- /dev/null
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project
+       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
+       xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+       <modelVersion>4.0.0</modelVersion>
+
+       <parent>
+               <groupId>org.simantics.g3d</groupId>
+               <artifactId>org.simantics.g3d.root</artifactId>
+               <version>1.0.0-SNAPSHOT</version>
+       </parent>
+
+       <artifactId>org.simantics.g3d.datastructures</artifactId>
+       <packaging>eclipse-plugin</packaging>
+       <version>1.0.0-SNAPSHOT</version>
+
+</project>
diff --git a/org.simantics.g3d.datastructures/src/org/simantics/g3d/datastructures/Box.java b/org.simantics.g3d.datastructures/src/org/simantics/g3d/datastructures/Box.java
new file mode 100644 (file)
index 0000000..ebe44e3
--- /dev/null
@@ -0,0 +1,32 @@
+package org.simantics.g3d.datastructures;
+
+import javax.vecmath.Point3d;
+
+public class Box {
+       
+       Point3d min;
+       Point3d max;
+       
+       public Point3d getMin() {
+               return min;
+       }
+       public Point3d getMax() {
+               return max;
+       }
+       
+       public Box(Point3d min, Point3d max) {
+               this.max = max;
+               this.min = min;
+       }
+       
+       public Box(double minx, double miny, double minz, double maxx, double maxy, double maxz) {
+               min = new Point3d(minx, miny, minz);
+               max = new Point3d(maxx, maxy, maxz);
+       }
+       
+       public Box(double min[], double max[]) {
+               this.min = new Point3d(min);
+               this.max = new Point3d(max);
+       }
+
+}
diff --git a/org.simantics.g3d.datastructures/src/org/simantics/g3d/datastructures/OcTree.java b/org.simantics.g3d.datastructures/src/org/simantics/g3d/datastructures/OcTree.java
new file mode 100644 (file)
index 0000000..671570c
--- /dev/null
@@ -0,0 +1,196 @@
+package org.simantics.g3d.datastructures;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.vecmath.Point3d;
+
+public class OcTree<T> {
+               
+               Point3d center;
+               Set<T> contains;
+               double dx,dy,dz;
+               
+               boolean leaf;
+               
+               OcTree<T>[] children;
+               
+               /**
+                * Creates a octree
+                * @param center center of the octree area
+                * @param dx size of the area along x-axis.
+                * @param dy size of the area along y-axis.
+                * @param dz size of the area along z-axis.
+                * @param depth depth of the tree structure. 
+                */
+               public OcTree(Point3d center, double dx, double dy, double dz, int depth) {
+                       this.center = center;
+                       this.dx = dx;
+                       this.dy = dy;
+                       this.dz = dz;
+                       this.leaf = true;
+                       split(depth);
+               }
+               
+               private OcTree(Point3d center, double dx, double dy, double dz) {
+                       this.center = center;
+                       this.dx = dx;
+                       this.dy = dy;
+                       this.dz = dz;
+                       this.leaf = true;
+               }
+               
+               /**
+                * Insets an object into the tree
+                * @param object
+                * @param bounds
+                */
+               public void insert(T object, Box bounds) {
+                       if (leaf) {
+                               contains.add(object);
+                       } else {
+                               
+                               boolean ins[] = getAccessArr(bounds);
+                               for (int i = 0; i < 8; i++) {
+                                       if (ins[i])
+                                               children[i].insert(object, bounds);
+                               }
+                       }
+               }
+               
+               private boolean[] getAccessArr(Box bounds) {
+                       boolean pX = bounds.getMin().getX() > center.getX();
+                       boolean nX = bounds.getMax().getX() < center.getX();
+                       boolean pY = bounds.getMin().getY() > center.getY();
+                       boolean nY = bounds.getMax().getY() < center.getY();
+                       boolean pZ = bounds.getMin().getZ() > center.getZ();
+                       boolean nZ = bounds.getMax().getZ() < center.getZ();
+                       
+                       boolean ins[] = new boolean[8];
+                       for (int i = 0; i < 8; i++)
+                               ins[i] = true;
+                       
+                       if (pX) {
+                               for (int i = 1; i < 8; i+=2) {
+                                       ins[i] = false;
+                               }
+                       } else if (nX) {
+                               for (int i = 0; i < 8; i+=2) {
+                                       ins[i] = false;
+                               }
+                       }
+                       
+                       if (pY) {
+                               for (int i = 2; i < 8; ) {
+                                       ins[i++] = false;
+                                       ins[i++] = false;
+                                       i+=2;
+                               }
+                       } else if (nY) {
+                               for (int i = 0; i < 8; ) {
+                                       ins[i++] = false;
+                                       ins[i++] = false;
+                                       i+=2;
+                               }
+                       }
+                       
+                       if (pZ) {
+                               for (int i = 4; i < 8; i++) {
+                                       ins[i] = false;
+                               }
+                       } else if (nZ) {
+                               for (int i = 0; i < 4; i++) {
+                                       ins[i] = false;
+                               }
+                       }
+                       return ins;
+               }
+               
+               /**
+                * Removes an object from the tree
+                * @param object
+                */
+               public void remove(T object) {
+                       if (leaf) {
+                               contains.remove(object);
+                       } else {
+                               for (OcTree<T> n : children) {
+                                       n.remove(object);
+                               }
+                       }
+               }
+               
+               /**
+                * Returns objects within the given area.
+                * @param bounds
+                * @param set
+                */
+               public void get(Box bounds, Set<T> set) {
+                       if (leaf) {
+                               set.addAll(contains);
+                       } else {
+                               
+                               boolean ins[] = getAccessArr(bounds);
+                               for (int i = 0; i < 8; i++) {
+                                       if (ins[i])
+                                               children[i].get(bounds, set);
+                               }
+                       }
+               }
+               
+               public void clear() {
+                       if (leaf) {
+                               contains.clear();
+                       } else {
+                               for (OcTree<T> n : children)
+                                       n.clear();
+                       }
+               }
+               
+               private void split(int depth) {
+                       if (!leaf) {
+                               throw new IllegalStateException("Node is already split");
+                       }
+                       if (depth <= 0) {
+                               this.contains = new HashSet<>();
+                               return;
+                       }
+                       split();
+                       depth--;
+                       for (OcTree<T> n : children) {
+                               n.split(depth);
+                       }
+               }
+               
+               
+               private void split() {
+                       double _dx = dx * 0.5;
+                       double _dy = dy * 0.5;
+                       double _dz = dz * 0.5;
+                       double xd2 = _dx * 0.5;
+                       double yd2 = _dy * 0.5;
+                       double zd2 = _dz * 0.5;
+                       children = new OcTree[8];
+                       children[0] = new OcTree<T>(new Point3d(center.x+xd2, center.y+yd2, center.z+zd2),_dx,_dy,_dz);
+                       children[1] = new OcTree<T>(new Point3d(center.x-xd2, center.y+yd2, center.z+zd2),_dx,_dy,_dz);
+                       children[2] = new OcTree<T>(new Point3d(center.x+xd2, center.y-yd2, center.z+zd2),_dx,_dy,_dz);
+                       children[3] = new OcTree<T>(new Point3d(center.x-xd2, center.y-yd2, center.z+zd2),_dx,_dy,_dz);
+                       children[4] = new OcTree<T>(new Point3d(center.x+xd2, center.y+yd2, center.z-zd2),_dx,_dy,_dz);
+                       children[5] = new OcTree<T>(new Point3d(center.x-xd2, center.y+yd2, center.z-zd2),_dx,_dy,_dz);
+                       children[6] = new OcTree<T>(new Point3d(center.x+xd2, center.y-yd2, center.z-zd2),_dx,_dy,_dz);
+                       children[7] = new OcTree<T>(new Point3d(center.x-xd2, center.y-yd2, center.z-zd2),_dx,_dy,_dz);
+                       leaf = false;
+               }
+               
+
+               public void dispose() {
+                       if (leaf) {
+                               this.contains = null;
+                       } else {
+                               for (OcTree<T> n : children) {
+                                       n.dispose();
+                               }
+                               children = null;
+                       }
+               }
+       }
index f094a076e8c10bb5c1b8376707a5c389c63857d8..ee9976b09604374e31fb5c1ac3ccc01b2c9c81d2 100644 (file)
@@ -181,4 +181,11 @@ This Agreement is governed by the laws of the State of New York and the intellec
          version="0.0.0"
          unpack="false"/>
 
+   <plugin
+         id="org.simantics.g3d.datastructures"
+         download-size="0"
+         install-size="0"
+         version="0.0.0"
+         unpack="false"/>
+
 </feature>
diff --git a/pom.xml b/pom.xml
index 75f7af22cc03a2b9c9482eca6d2317d68d188450..4e93b310a5383c25d75aa8f5f1f0dc44f098d6b0 100644 (file)
--- a/pom.xml
+++ b/pom.xml
                <module>org.simantics.g3d.csg.ontology</module>
                <module>org.simantics.g3d.ontology</module>
                <module>org.simantics.g3d.vtk</module>
+               <module>org.simantics.g3d.datastructures</module>
                <module>org.simantics.opencascade</module>
                <module>org.simantics.opencascade.vtk</module>
                <!--<module>org.simantics.g3d.jme</module>-->