Skip to content

Commit

Permalink
Start on sentinel spatial pub/sub
Browse files Browse the repository at this point in the history
  • Loading branch information
Hellblazer committed Aug 27, 2023
1 parent bf30eaa commit 5d58ad5
Show file tree
Hide file tree
Showing 11 changed files with 261 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,36 @@ public static void centerSphere(float[] pa, float[] pb, float[] pc, float[] pd,
centerSphere(pa[0], pa[1], pa[2], pb[0], pb[1], pb[2], pc[0], pc[1], pc[2], pd[0], pd[1], pd[2], po);
}

/**
* Computes the center of the sphere defined by the points a, b, c, and d. The
* latter are assumed to be in CCW order, such that the method
* {@link #leftOfPlane} would return a positive number.
*
* @param po array containing (x,y,z) coordinates of center.
*/
public static void centerSphereFast(float xa, float ya, float za, float xb, float yb, float zb, float xc, float yc,
float zc, float xd, float yd, float zd, float[] po) {
double adx = xa - xd;
double bdx = xb - xd;
double cdx = xc - xd;
double ady = ya - yd;
double bdy = yb - yd;
double cdy = yc - yd;
double adz = za - zd;
double bdz = zb - zd;
double cdz = zc - zd;
double ads = adx * adx + ady * ady + adz * adz;
double bds = bdx * bdx + bdy * bdy + bdz * bdz;
double cds = cdx * cdx + cdy * cdy + cdz * cdz;
double scale = 0.5 / leftOfPlaneFast(xa, ya, za, xb, yb, zb, xc, yc, zc, xd, yd, zd);
po[0] = xd + (float) (scale
* (ads * (bdy * cdz - cdy * bdz) + bds * (cdy * adz - ady * cdz) + cds * (ady * bdz - bdy * adz)));
po[1] = yd + (float) (scale
* (ads * (bdz * cdx - cdz * bdx) + bds * (cdz * adx - adz * cdx) + cds * (adz * bdx - bdz * adx)));
po[2] = zd + (float) (scale
* (ads * (bdx * cdy - cdx * bdy) + bds * (cdx * ady - adx * cdy) + cds * (adx * bdy - bdx * ady)));
}

public static Point3d[] findLineSphereIntersections(Tuple3d linePoint0, Tuple3d linePoint1, Tuple3d circleCenter,
double circleRadius) {
// http://www.codeproject.com/Articles/19799/Simple-Ray-Tracing-in-C-Part-II-Triangles-Intersec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public boolean includes(Node node) {
}

@Override
public void insert(Node id, Tuple3f point3f) {
public void insert(Node id) {
// TODO Auto-generated method stub

}
Expand All @@ -78,12 +78,6 @@ public boolean isEnclosing(Node node, Node center_node_id) {
return false;
}

@Override
public boolean overlaps(Node node, Tuple3f center, float radiusSquared) {
// TODO Auto-generated method stub
return false;
}

@Override
public void remove(Node node) {
// TODO Auto-generated method stub
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

package com.hellblazer.luciferase.sentinel;

import static com.hellblazer.luciferase.sentinel.Geometry.centerSphere;
import static com.hellblazer.luciferase.sentinel.Geometry.centerSphereFast;
import static com.hellblazer.luciferase.sentinel.V.A;
import static com.hellblazer.luciferase.sentinel.V.B;
import static com.hellblazer.luciferase.sentinel.V.C;
Expand Down Expand Up @@ -668,7 +668,7 @@ public void addFaces(List<Vertex[]> faces) {

public Point3f center() {
float[] center = new float[3];
centerSphere(a.x, a.y, a.z, b.x, b.y, b.z, c.x, c.y, c.z, d.x, d.y, d.z, center);
centerSphereFast(a.x, a.y, a.z, b.x, b.y, b.z, c.x, c.y, c.z, d.x, d.y, d.z, center);
return new Point3f(center[0], center[1], center[2]);
}

Expand Down Expand Up @@ -1212,7 +1212,7 @@ void traverseVoronoiFace(Tetrahedron origin, Tetrahedron from, Vertex vC, Vertex
return;
}
float[] center = new float[3];
centerSphere(a.x, a.y, a.z, b.x, b.y, b.z, c.x, c.y, c.z, d.x, d.y, d.z, center);
centerSphereFast(a.x, a.y, a.z, b.x, b.y, b.z, c.x, c.y, c.z, d.x, d.y, d.z, center);
face.add(new Point3f(center[0], center[1], center[2]));
V next = VORONOI_FACE_NEXT[ordinalOf(from).ordinal()][ordinalOf(vC).ordinal()][ordinalOf(axis).ordinal()];
Tetrahedron t = getNeighbor(next);
Expand All @@ -1234,7 +1234,7 @@ void traverseVoronoiFace(Tetrahedron origin, Tetrahedron from, Vertex vC, Vertex
void traverseVoronoiFace(Vertex vC, Vertex axis, List<Tuple3f[]> faces) {
ArrayList<Point3f> face = new ArrayList<>();
float[] center = new float[3];
centerSphere(a.x, a.y, a.z, b.x, b.y, b.z, c.x, c.y, c.z, d.x, d.y, d.z, center);
centerSphereFast(a.x, a.y, a.z, b.x, b.y, b.z, c.x, c.y, c.z, d.x, d.y, d.z, center);
face.add(new Point3f(center[0], center[1], center[2]));
V v = VORONOI_FACE_ORIGIN[ordinalOf(vC).ordinal()][ordinalOf(axis).ordinal()];
Tetrahedron next = getNeighbor(v);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright (C) 2023 Hal Hildebrand. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.hellblazer.luciferase.sentinel.cast;

import javax.vecmath.Point3f;
import javax.vecmath.Tuple3f;

/**
* @author hal.hildebrand
*/
public class AbstractSpatial {

protected Point3f location;

public AbstractSpatial(Point3f location) {
this.location = location;
}

public Point3f getLocation() {
return location;
}

public Point3f moveBy(Tuple3f delta) {
location.add(delta);
return location;
}

public void setLocation(Point3f location) {
this.location = location;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (C) 2023 Hal Hildebrand. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.hellblazer.luciferase.sentinel.cast;

import javax.vecmath.Point3f;

/**
* @author hal.hildebrand
*/
public class SpatialPublish extends AbstractSpatial {

public SpatialPublish(Point3f location) {
super(location);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (C) 2023 Hal Hildebrand. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.hellblazer.luciferase.sentinel.cast;

import javax.vecmath.Point3f;

/**
* @author hal.hildebrand
*/
abstract public class SpatialSubscription extends AbstractSpatial {

public SpatialSubscription(Point3f location) {
super(location);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright (C) 2023 Hal Hildebrand. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.hellblazer.luciferase.sentinel.cast;

import javax.vecmath.Point3f;

/**
* @author hal.hildebrand
*/
public class SphericalPublish extends SpatialPublish {
protected float radius;
protected float radiusSquared;

public SphericalPublish(Point3f location, float radius) {
super(location);
this.radius = radius;
this.radiusSquared = radius * radius;
}

public float getRadius() {
return radius;
}

public float getRadiusSquared() {
return radiusSquared;
}

public void setRadius(float radius) {
this.radius = radius;
radiusSquared = radius * radius;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright (C) 2023 Hal Hildebrand. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.hellblazer.luciferase.sentinel.cast;

import javax.vecmath.Point3f;

/**
* @author hal.hildebrand
*/
public class SphericalSubscription extends SpatialSubscription {
protected float radius;
protected float radiusSquared;

public SphericalSubscription(Point3f location, float radius) {
super(location);
this.radius = radius;
this.radiusSquared = radius * radius;
}

public float getRadius() {
return radius;
}

public float getRadiusSquared() {
return radiusSquared;
}

public void setRadius(float radius) {
this.radius = radius;
radiusSquared = radius * radius;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;

import javax.vecmath.Point3f;
import javax.vecmath.Tuple3f;
import javax.vecmath.Vector3f;

import com.hellblazer.luciferase.sentinel.Vertex;
import com.hellblazer.luciferase.thoth.Movable;
import com.hellblazer.luciferase.thoth.Perceiving;
import com.hellblazer.primeMover.annotations.Entity;
Expand All @@ -55,8 +57,18 @@ public Perceptron(E entity, Point3f location, int aoiRadius, int maximumVelocity

@Override
public Node closestTo(Tuple3f point3f) {
// TODO Auto-generated method stub
return null;
Vertex current = null;
float dSqur = 0;
for (var v : locate(point3f, ThreadLocalRandom.current()).getVertices()) {
var d = new Vector3f(point3f);
d.sub(v);
final var dist = d.lengthSquared();
if (current == null || dist < dSqur) {
current = v;
dSqur = dist;
}
}
return (Node) current;
}

@Override
Expand All @@ -66,7 +78,6 @@ public void fadeFrom(Node neighbor) {

@Override
public Node getAliased(Node node) {
// TODO Auto-generated method stub
return null;
}

Expand All @@ -88,7 +99,7 @@ public boolean includes(Node node) {
}

@Override
public void insert(Node id, Tuple3f point3f) {
public void insert(Node id) {
// TODO Auto-generated method stub

}
Expand Down Expand Up @@ -148,7 +159,7 @@ public void moveBoundary(Node neighbor) {

if (!includes(neighbor)) {
if (overlaps(this, neighbor.getLocation(), maxRadiusSquared)) {
insert(neighbor, neighbor.getLocation());
insert(neighbor);
}
return;
}
Expand Down Expand Up @@ -182,20 +193,14 @@ public void noticeNodes(Collection<Node> nodes) {

for (var peer : nodes) {
if (!includes(peer)) {
insert(peer.clone(), peer.getLocation());
insert(peer.clone());
if (overlaps(this, peer.getLocation(), peer.getMaximumRadiusSquared())) {
peer.perceive(this);
}
}
}
}

@Override
public boolean overlaps(Node node, Tuple3f center, float radiusSquared) {
// TODO Auto-generated method stub
return false;
}

@Override
public void perceive(Node neighbor) {
if (!active) {
Expand Down Expand Up @@ -242,7 +247,7 @@ public void update(Node node, Tuple3f coord) {
}

protected void add(Node node) {
insert(node.clone(), node.getLocation());
insert(node.clone());
}

protected void handshakeWith(Node node) {
Expand Down Expand Up @@ -305,7 +310,7 @@ protected void removeNonOverlapped() {
protected Point3f update(Node node) {
var neighbor = getAliased(node);
if (neighbor == null) {
insert(node.clone(), node.getLocation());
insert(node.clone());
return null;
}
Point3f oldLocation = new Point3f(neighbor.getLocation());
Expand Down
Loading

0 comments on commit 5d58ad5

Please sign in to comment.