Skip to content

Commit

Permalink
Fixes #3397. Fixes #3391.
Browse files Browse the repository at this point in the history
Fixes an undocumented issue where the camera location would be negated
in y. Improves a bit the presentation of the Physics models. Prepares
some GLCanvas debug facilities for the next round of issues.
  • Loading branch information
AlexisDrogoul committed May 8, 2022
1 parent dcc3bf6 commit 1a1440f
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 65 deletions.
@@ -1,13 +1,13 @@

/*******************************************************************************************************
*
* CompoundSpatialIndex.java, in msi.gama.core, is part of the source code of the
* GAMA modeling and simulation platform (v.1.8.2).
* CompoundSpatialIndex.java, in msi.gama.core, is part of the source code of the GAMA modeling and simulation platform
* (v.1.8.2).
*
* (c) 2007-2022 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
*
********************************************************************************************************/
package msi.gama.metamodel.topology;

Expand Down Expand Up @@ -106,6 +106,7 @@ public IAgent firstAtDistance(final IScope scope, final IShape source, final dou
try (final Collector.AsList<IAgent> shapes = Collector.getList()) {
for (final double step : steps) {
for (final ISpatialIndex si : indices) {
if (si == null) { continue; }
final IAgent first = si.firstAtDistance(scope, source, step, f);
if (first != null) { shapes.add(first); }
}
Expand Down Expand Up @@ -193,7 +194,9 @@ public Collection<IAgent> allAtDistance(final IScope scope, final IShape source,
if (disposed) return Collections.EMPTY_LIST;
Iterable<ISpatialIndex> indices = add(scope, f);
try (final ICollector<IAgent> agents = Collector.getOrderedSet()) {
for (final ISpatialIndex si : indices) { agents.addAll(si.allAtDistance(scope, source, dist, f)); }
for (final ISpatialIndex si : indices) {
if (si != null) { agents.addAll(si.allAtDistance(scope, source, dist, f)); }
}
agents.shuffleInPlaceWith(scope.getRandom());
return agents.items();
}
Expand Down Expand Up @@ -230,7 +233,7 @@ private ISpatialIndex add(final IScope scope, final ISpecies species, final bool
* @return the i spatial index
*/
private ISpatialIndex add(final IPopulation<? extends IAgent> pop, final boolean insertAgents) {
if (disposed) return null;
if (disposed || pop == null) return null;
ISpecies spec = pop.getSpecies();
ISpatialIndex index = spatialIndexes.getOrDefault(spec, null);
if (index == null) {
Expand Down Expand Up @@ -283,10 +286,10 @@ public void remove(final ISpecies species) {
public void update(final IScope scope, final Envelope envelope, final boolean parallel) {
this.bounds = envelope;
this.parallel = parallel;

final WeakHashMap<ISpecies, ISpatialIndex> spatialIndexesTmp = new WeakHashMap<>();
spatialIndexesTmp.putAll(spatialIndexes);

for (ISpecies species : spatialIndexesTmp.keySet()) {
remove(species);
add(scope, species, true);
Expand Down
21 changes: 7 additions & 14 deletions msi.gama.core/src/msi/gama/util/random/GamaRNG.java
@@ -1,12 +1,11 @@
/*******************************************************************************************************
*
* GamaRNG.java, in msi.gama.core, is part of the source code of the
* GAMA modeling and simulation platform (v.1.8.2).
* GamaRNG.java, in msi.gama.core, is part of the source code of the GAMA modeling and simulation platform (v.1.8.2).
*
* (c) 2007-2022 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
*
********************************************************************************************************/
package msi.gama.util.random;

Expand Down Expand Up @@ -46,27 +45,23 @@ public GamaRNG(final long seed) {
* Instantiates a new gama RNG.
*/
public GamaRNG() {
super();
}

/**
* Gets the number of times the generator has been asked to draw a random number.
*
* @return the number of times the generator has been asked to draw a random number
*/
public int getUsage() {
return usage;
}
public int getUsage() { return usage; }

/**
* Sets the number of times the generator has been asked to draw a random number.
*
* @param usage the new number of times the generator has been asked to draw a random number
* @param usage
* the new number of times the generator has been asked to draw a random number
*/
public void setUsage(final int usage) {
for (long i = 0; i < usage; i++) {
next(32);
}
for (long i = 0; i < usage; i++) { next(32); }
}

/**
Expand Down Expand Up @@ -96,9 +91,7 @@ public static int convertBytesToInt(final byte[] bytes, final int offset) {
public static int[] convertBytesToInts(final byte[] bytes) {
if (bytes.length % 4 != 0) throw new IllegalArgumentException("Number of input bytes must be a multiple of 4.");
final int[] ints = new int[bytes.length / 4];
for (int i = 0; i < ints.length; i++) {
ints[i] = convertBytesToInt(bytes, i * 4);
}
for (int i = 0; i < ints.length; i++) { ints[i] = convertBytesToInt(bytes, i * 4); }
return ints;
}

Expand Down
Expand Up @@ -125,7 +125,7 @@ experiment "Corridor" type: gui autorun: true {
button_location <- {simulation.corridor_left_bounds + 100, 100};
}
output {
display defaut_display background: #black fullscreen: true toolbar: false {
display defaut_display type: java2D background: #black fullscreen: true toolbar: false {
graphics back {
draw shape color: #black wireframe: false;
draw corridor_wall_0_display color: #gray wireframe: true;
Expand Down Expand Up @@ -153,16 +153,16 @@ experiment "Corridor" type: gui autorun: true {

graphics button {
draw circle(50) color: #darkgray at: button_location;
draw circle(40) color: !capture_pedestrians ? (button_hover ? #red : #darkred) : (button_hover ? #lightgreen : #darkgreen) at: button_location;
draw circle(40) color: !capture_pedestrians ? (button_hover ? #yellow : #red) : (button_hover ? #lightgreen : #darkgreen) at: button_location;
}

event mouse_down {
if (button_location distance_to #user_location <= 40) {
if (button_location distance_to #user_location <= 50) {
capture_pedestrians <- !capture_pedestrians;
}
}
event mouse_move {
button_hover <- (button_location distance_to #user_location <= 40);
button_hover <- (button_location distance_to #user_location <= 50);
}
}
}
Expand Down
Expand Up @@ -15,7 +15,7 @@ model Vulcano
* to manage physical agents. In this model, the world itself is not a physical agent
*/
global parent: physical_world {
bool use_native_library <- true;
bool use_native <- true;
//Step (in #sec) passed to the physics engine. The same step is used for the simulation and the physics engine. The accuracy and synchronization
//between the two can be controlled by max_substeps. A too large step (e.g. 1#sec) would make the lava 'pass through' the ground (tunnel effect).
//A too small (e.g. 0.01), while more accurate, would, given the velocity of the lava, slow everything down on useless computations.
Expand Down Expand Up @@ -51,10 +51,6 @@ grid patches file: grid_file("../images/DEM/Volcano DEM.asc") skills: [static_bo
// When redefined, it allows agents to react to contacts. Here, every new contact with a lava agent makes a patch decrease its height (grid_value)
// and that of its neigbors by a small amount, as well as stop the lava agent quite brutally (clearing all the forces applied to it) to imitate "stickiness"
action contact_added_with (agent other) {
ask lava(other) {
// write "Collision of " + myself + " with " + self;
// do apply clearance: true;
}
if (erosion) {
grid_value <- grid_value - 0.01;
ask neighbors {
Expand Down
Expand Up @@ -8,7 +8,7 @@
model Terrain

global parent: physical_world {

bool use_native <- true;
// We scale the DEM up a little
float z_scale <- 0.5;
float step <- 1.0/30;
Expand Down Expand Up @@ -59,7 +59,7 @@ species water skills: [dynamic_body] {

experiment "3D view" type: gui {

string camera_loc <- #from_above;
string camera_loc <- #from_up_front;
int distance <- 200;

action _init_ {
Expand All @@ -68,7 +68,7 @@ experiment "3D view" type: gui {
create simulation with: [z_scale::2.0];
create simulation with: [z_scale::3.0];
}
parameter "Location of the camera" var: camera_loc among: [#from_above, #from_up_left, #from_up_right, #from_up_front];
parameter "Location of the camera" var: camera_loc among: [#from_up_front, #from_above, #from_up_left, #from_up_right];
parameter "Distance of the camera" var: distance min: 1 max: 1000 slider: true;
parameter "Number of water agents per cycle" var: number_of_water_units;

Expand Down
Expand Up @@ -11,6 +11,7 @@ model FallingHelloWorld


global parent: physical_world {
bool use_native <- true;
float restitution <- 0.8; // the "bounciness" of the world
float friction <- 0.2; // the deceleration it imposes on other objects
int environment_size <- 500;
Expand Down
Expand Up @@ -13,6 +13,7 @@ model Gas
* to manage physical agents. In this model, the world itself is not a physical agent
*/
global parent: physical_world {
bool use_native <- true;
int width parameter: 'Dimensions' init:500 ;
point gravity <- {0.0, 0.0, 0.0};
bool withGravity <- false parameter: "Enable gravity" on_change: {gravity <- withGravity ? {0.0, 0.0, -9.81} : {0.0, 0.0, 0.0};};
Expand Down
Expand Up @@ -15,7 +15,7 @@ model pool3D
*/
global parent: physical_world {
// The dynamics of the agents is a bit different if we use the native (3.0.x) or Java version (2.8.x) of Bullet
bool use_native_library <- false;
bool use_native <- false;
string library <- "bullet";
//All the physical characteristics of the balls can be accessed here and modified at will by the user
float ball_damping <- 0.05 min: 0.0 max: 1.0 on_change: {ask ball {damping<-ball_damping;}};
Expand Down
Expand Up @@ -7,6 +7,7 @@
model Stairs

global parent: physical_world {
bool use_native <- true;
list<string> textures <- ["tennis", "foot", "basket", "ball"];
list<float> sizes <- [0.07, 0.22, 0.25, 0.5];
list<float> masses <- [0.1, 0.45, 0.62, 0.01];
Expand All @@ -15,7 +16,7 @@ global parent: physical_world {
int tennis <- 0;
int basket <- 1;
int foot <- 2;
float step <- 0.003;
float step <- 0.001;
int max_substeps <- 0;
int number_of_steps <- 8;
float max_height <- 20.0;
Expand All @@ -40,7 +41,7 @@ global parent: physical_world {

reflex when: every(500 #cycle) {
create ball with: [type::one_of(0, 1, 2, 3)] {
location <- {dimension * 3 / 2 + rnd(2) - 1, dimension + rnd(2) - 1, max_height + dimension};
location <- {dimension * 3 / 2 + rnd(4) - 2, dimension + rnd(4) - 2, max_height + dimension + rnd(4) - 2};
}

}
Expand Down Expand Up @@ -85,8 +86,9 @@ species ball skills: [dynamic_body] {
experiment Stairs type: gui {
output {
display "Climb" type: opengl axes: false {
camera 'default' location: {-47.816,165.8396,88.7059} target: {60.0,60.0,0.0};
graphics ground {
draw shape color: rgb(122, 140, 70, 255);
draw shape color: #blue;
//draw aabb wireframe: true color: #blue;
}

Expand Down
Expand Up @@ -164,9 +164,7 @@ public PhysicalSimulationAgent(final IPopulation<? extends IAgent> s, final int
public Object primRegister(final IScope scope) {
IList<IAgent> agents = scope.getListArg(BODIES);
if (agents == null) return null;
for (IAgent agent : agents) {
registerAgent(scope, agent);
}
for (IAgent agent : agents) { registerAgent(scope, agent); }
return agents;
}

Expand Down Expand Up @@ -210,9 +208,7 @@ public void updateAgent(final IScope scope, final IAgent agent) {
* @return the terrain
*/
@getter (IPhysicalConstants.TERRAIN)
public IField getTerrain() {
return terrain;
}
public IField getTerrain() { return terrain; }

/**
* Sets the terrain.
Expand All @@ -221,9 +217,7 @@ public IField getTerrain() {
* the new terrain
*/
@setter (IPhysicalConstants.TERRAIN)
public void setTerrain(final IField t) {
terrain = t;
}
public void setTerrain(final IField t) { terrain = t; }

/**
* Gets the ccd.
Expand Down Expand Up @@ -289,8 +283,7 @@ public void setAutomatedRegistration(final IScope scope, final Boolean v) {
* @return the boolean
*/
@getter (
value = USE_NATIVE,
initializer = true)
value = USE_NATIVE)
public Boolean usesNativeLibrary(final IScope scope) {
if (useNativeLibrary == null) { useNativeLibrary = NativeLoader.LoadNativeLibrary(); }
return useNativeLibrary;
Expand All @@ -307,7 +300,7 @@ public Boolean usesNativeLibrary(final IScope scope) {
@setter (USE_NATIVE)
public void useNativeLibrary(final IScope scope, final Boolean v) {
// If we have not successfully loaded the library, then the setting should remain false.
useNativeLibrary = NativeLoader.LoadNativeLibrary() && v;
useNativeLibrary = v && NativeLoader.LoadNativeLibrary();
}

/**
Expand Down Expand Up @@ -467,29 +460,21 @@ public void notifyAgentsAdded(final IScope scope, final IPopulation<? extends IA
final Collection<? extends IAgent> agents) {
if (scope.interrupted()) return;

if (automatedRegistration) {
for (IAgent a : agents) {
registerAgent(scope, a);
}
}
if (automatedRegistration) { for (IAgent a : agents) { registerAgent(scope, a); } }
}

@Override
public void notifyAgentsRemoved(final IScope scope, final IPopulation<? extends IAgent> pop,
final Collection<? extends IAgent> agents) {
if (scope.interrupted()) return;

for (IAgent a : agents) {
unregisterAgent(scope, a);
}
for (IAgent a : agents) { unregisterAgent(scope, a); }
}

@Override
public void notifyPopulationCleared(final IScope scope, final IPopulation<? extends IAgent> pop) {
if (scope.interrupted()) return;
for (IAgent a : pop) {
unregisterAgent(scope, a);
}
for (IAgent a : pop) { unregisterAgent(scope, a); }

}

Expand Down
Expand Up @@ -1365,9 +1365,9 @@ public void toggleCamera() {
@Override
public String getCameraDefinition() {
StringBuilder text = new StringBuilder(IKeyword.CAMERA).append(" 'default' ").append(IKeyword.LOCATION)
.append(": ").append(new GamaPoint(data.getCameraPos()).withPrecision(4).serialize(false));
.append(": ").append(new GamaPoint(data.getCameraPos()).yNegated().withPrecision(4).serialize(false));
text.append(" ").append(IKeyword.TARGET).append(": ")
.append(new GamaPoint(data.getCameraTarget()).withPrecision(4).serialize(false)).append(";");
.append(new GamaPoint(data.getCameraTarget()).yNegated().withPrecision(4).serialize(false)).append(";");
return text.toString();
}

Expand Down
12 changes: 10 additions & 2 deletions ummisco.gama.opengl/src/ummisco/gama/opengl/view/GamaGLCanvas.java
Expand Up @@ -68,16 +68,21 @@ public class GamaGLCanvas extends Composite implements GLAutoDrawable, IDelegate
/** The detached. */
protected boolean detached = false;

final String name;

/**
* Instantiates a new gama GL canvas.
*
* @param parent
* the parent
* @param renderer
* the renderer
* @param name
* for debug purposes
*/
public GamaGLCanvas(final Composite parent, final IOpenGLRenderer renderer) {
public GamaGLCanvas(final Composite parent, final IOpenGLRenderer renderer, final String name) {
super(parent, SWT.NONE);
this.name = name;
parent.setLayout(new FillLayout());
this.setLayout(new FillLayout());
final var cap = defineCapabilities();
Expand Down Expand Up @@ -325,6 +330,7 @@ public Window getNEWTWindow() {
* Reparent window.
*/
public void reparentWindow() {
DEBUG.OUT("Entering making GLWindow " + name + " reparent ");
if (!FLAGS.USE_NATIVE_OPENGL_WINDOW) return;
final Window w = (Window) drawable;
if (setWindowVisible(false)) {
Expand All @@ -341,11 +347,13 @@ public void reparentWindow() {
* the new window visible
*/
public boolean setWindowVisible(final boolean b) {
DEBUG.OUT("Entering making GLWindow " + name + " visible " + b);
if (!FLAGS.USE_NATIVE_OPENGL_WINDOW) return false;
final Window w = (Window) drawable;
if (!w.isNativeValid()) return false;
DEBUG.OUT("Make GLWindow visible: " + b);
DEBUG.OUT("Make GLWindow " + name + " visible: " + b);
w.setVisible(b);
DEBUG.OUT("Make GLWindow " + name + " visible " + b + " succeeded");
return true;
}

Expand Down

0 comments on commit 1a1440f

Please sign in to comment.