From 83cc2c6d7dedc6431850d44f183a59a5162755dd Mon Sep 17 00:00:00 2001 From: Daniel Bisig Date: Wed, 20 Jul 2022 11:28:30 +0200 Subject: [PATCH] added example and Readme --- README.md | 69 +-- example/addons.make | 9 + example/bin/data/shaders/agent_shader.frag | 9 + example/bin/data/shaders/agent_shader.vert | 15 + example/bin/data/shaders/grid_shader.frag | 9 + example/bin/data/shaders/grid_shader.vert | 15 + example/bin/data/shaders/neighbor_shader.frag | 9 + example/bin/data/shaders/neighbor_shader.vert | 15 + example/bin/data/shaders/trail_shader.frag | 11 + example/bin/data/shaders/trail_shader.vert | 18 + example/config.make | 140 ++++++ example/src/main.cpp | 16 + example/src/ofApp.cpp | 401 ++++++++++++++++++ example/src/ofApp.h | 24 ++ 14 files changed, 732 insertions(+), 28 deletions(-) create mode 100644 example/addons.make create mode 100644 example/bin/data/shaders/agent_shader.frag create mode 100644 example/bin/data/shaders/agent_shader.vert create mode 100644 example/bin/data/shaders/grid_shader.frag create mode 100644 example/bin/data/shaders/grid_shader.vert create mode 100644 example/bin/data/shaders/neighbor_shader.frag create mode 100644 example/bin/data/shaders/neighbor_shader.vert create mode 100644 example/bin/data/shaders/trail_shader.frag create mode 100644 example/bin/data/shaders/trail_shader.vert create mode 100644 example/config.make create mode 100644 example/src/main.cpp create mode 100644 example/src/ofApp.cpp create mode 100644 example/src/ofApp.h diff --git a/README.md b/README.md index 16eff96..bd8c58b 100644 --- a/README.md +++ b/README.md @@ -1,45 +1,58 @@ -**Edit a file, create a new file, and clone from Bitbucket in under 2 minutes** +## ofxDabFlock -When you're done, you can delete the content in this README and update the file with details for others getting started with your repository. +**Author**: Daniel Bisig - Coventry University, UK - [ad5041@coventry.ac.uk](ad5041@coventry.ac.uk) - Zurich University of the Arts, CH - [daniel.bisig@zhdk.ch](daniel.bisig@zhdk.ch) -*We recommend that you open this README in another tab as you perform the tasks below. You can [watch our video](https://youtu.be/0ocf7u76WSo) for a full demo of all the steps in this tutorial. Open the video in a new tab to avoid leaving Bitbucket.* +**Dependencies**: [ofxDabBase](https://bitbucket.org/dbisig/ofxdabbase_011/src/master/), [ofxDabEvent](https://bitbucket.org/dbisig/ofxdabevent_011/src/master/), [ofxDabMath](https://bitbucket.org/dbisig/ofxdabmath_011/src/master/), [ofxDabGeom](https://bitbucket.org/dbisig/ofxdabgeom_011/src/master/), [ofxDabOsc](https://bitbucket.org/dbisig/ofxdabosc_011/src/master/), [ofxDabSpace](https://bitbucket.org/dbisig/ofxdabspace_011/src/master/), [ofxAssimpModelLoader](https://openframeworks.cc/documentation/ofxAssimpModelLoader/ofxAssimpModelLoader/), [ofxJSON](https://github.com/jeffcrouse/ofxJSON) --- -## Edit a file +## Summary -You’ll start by editing this README file to learn how to edit a file in Bitbucket. +ofxDabFlock is a framework that supports the creation of flocking simulations. ofxDabFlock is highly generic in it that it doesn't impose any restrictions on the number, type and dimensionality of parameters that agents can possess. ofxDabFlock provides functions to calculate spatial relationships among parameters as well as between parameters and other spatial structures such as splines or vector-fields. Agent behaviours establish functional relationships among these parameters. ofxDabFlock can communicate via OSC messages with other applications. OSC messages can be used by other applications to receive the values of simulation parameters. In addition, OSC messages can also be employed as commands to remotely configure and control the simulation. Finally, ofxDabFlock provides simple rendering functionality to visualise the simulation. The code is compatible with OpenFrameworks 0.11 and has been tested on Windows and MacOS. -1. Click **Source** on the left side. -2. Click the README.md link from the list of files. -3. Click the **Edit** button. -4. Delete the following text: *Delete this line to make a change to the README from Bitbucket.* -5. After making your change, click **Commit** and then **Commit** again in the dialog. The commit page will open and you’ll see the change you just made. -6. Go back to the **Source** page. +### Entities ---- +**Agent**: an agent is a single autonomous entity that is typically part of a larger group. And agent possesses properties and behaviours. -## Create a file +**Swarm**: a swarm is a collection of agents that typically identical the same properties and behaviours. Agents can be removed or added from a swarm. The swarm can also possess its own properties and behaviours that are independent of those of the agents. -Next, you’ll add a new file to this repository. +**Env**: an environment is a space that is discretised as a grid with each grid. An environment can possess its own properties and behaviours. Its properties can possess different values at each grid location. -1. Click the **New file** button at the top of the **Source** page. -2. Give the file a filename of **contributors.txt**. -3. Enter your name in the empty file space. -4. Click **Commit** and then **Commit** again in the dialog. -5. Go back to the **Source** page. +### Properties -Before you move on, go ahead and explore the repository. You've already seen the **Source** page, but check out the **Commits**, **Branches**, and **Settings** pages. +**Parameter**: represents a named property of an agent or swarm and stores an array of floating point values. Neighbourhood relationships are based on the Euclidian distance between parameters. ---- +**EnvParameter**: represents a named property of an environment. Stores the values of the parameter at each cell position for a spatial grid. + +**ParameterList**: a collection of all parameters of an agent, swarm, or environment. + +### Behaviours + +**Behavior**: represents a named behaviour of an agent or swarm. Behaviours operate on input and output parameters, and overwrites the values of the latter based on the values of the former. Depending on the behaviour, neighbourhood relationships between parameters are taken into account or not. + +**EnvBehavior**: represents a named behaviour of an environment. + +**BehaviorList**: a collection of all behaviours of an agent, swarm, or environment. + +### Communication + +**FlockCom**: manages all communication with a flocking simulation. Allows to register parameters whose values are then sent via OSC to a listening port. + +**OscControl**: handles the remote control of a flocking simulation via OSC messages. + +### Serialisation + +**SerializeTools**: The configuration and state of a flocking simulation can be saved as JSON files and later on restored from these files. + +### Visualisation + +**FlockVisuals**: provides simple functionality for visualisation the flocking simulation. + +**VisAgentShape**: draws agents as geometrical object -## Clone a repository +**VisAgentTrail**: draws the trajectory of an agent parameter as polyline -Use these steps to clone from SourceTree, our client for using the repository command-line free. Cloning allows you to work on your files locally. If you don't yet have SourceTree, [download and install first](https://www.sourcetreeapp.com/). If you prefer to clone from the command line, see [Clone a repository](https://confluence.atlassian.com/x/4whODQ). +**VisNeighborSpace**: draws neigbourhood relationships between parameters as lines -1. You’ll see the clone button under the **Source** heading. Click that button. -2. Now click **Check out in SourceTree**. You may need to create a SourceTree account or log in. -3. When you see the **Clone New** dialog in SourceTree, update the destination path and name if you’d like to and then click **Clone**. -4. Open the directory you just created to see your repository’s files. +**VisGridSpace**: draws the values at the grid positions of an environment grid as lines -Now that you're more familiar with your Bitbucket repository, go ahead and add a new file locally. You can [push your change back to Bitbucket with SourceTree](https://confluence.atlassian.com/x/iqyBMg), or you can [add, commit,](https://confluence.atlassian.com/x/8QhODQ) and [push from the command line](https://confluence.atlassian.com/x/NQ0zDQ). \ No newline at end of file diff --git a/example/addons.make b/example/addons.make new file mode 100644 index 0000000..c00054b --- /dev/null +++ b/example/addons.make @@ -0,0 +1,9 @@ +ofxAssimpModelLoader +ofxDabBase +ofxDabEvent +ofxDabMath +ofxDabGeom +ofxDabOsc +ofxDabSpace +ofxJSON +ofxDabFlock diff --git a/example/bin/data/shaders/agent_shader.frag b/example/bin/data/shaders/agent_shader.frag new file mode 100644 index 0000000..8fced64 --- /dev/null +++ b/example/bin/data/shaders/agent_shader.frag @@ -0,0 +1,9 @@ +#version 330 + +uniform vec4 Color; +out vec4 outputColor; + +void main() +{ + outputColor = Color; +} \ No newline at end of file diff --git a/example/bin/data/shaders/agent_shader.vert b/example/bin/data/shaders/agent_shader.vert new file mode 100644 index 0000000..c1910b8 --- /dev/null +++ b/example/bin/data/shaders/agent_shader.vert @@ -0,0 +1,15 @@ +#version 330 + +// these are for the programmable pipeline system and are passed in +// by default from OpenFrameworks +layout(location = 0) in vec4 vertexPosition_ModelSpace; + +uniform vec4 Color; +uniform mat4 ProjectionMatrix; +uniform mat4 ModelMatrix; +uniform mat4 ViewMatrix; + +void main() +{ + gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix * vertexPosition_ModelSpace; +} \ No newline at end of file diff --git a/example/bin/data/shaders/grid_shader.frag b/example/bin/data/shaders/grid_shader.frag new file mode 100644 index 0000000..c789db6 --- /dev/null +++ b/example/bin/data/shaders/grid_shader.frag @@ -0,0 +1,9 @@ +#version 330 + +uniform vec4 Color; +out vec4 outputColor; + +void main() +{ + outputColor = Color; +} \ No newline at end of file diff --git a/example/bin/data/shaders/grid_shader.vert b/example/bin/data/shaders/grid_shader.vert new file mode 100644 index 0000000..c1910b8 --- /dev/null +++ b/example/bin/data/shaders/grid_shader.vert @@ -0,0 +1,15 @@ +#version 330 + +// these are for the programmable pipeline system and are passed in +// by default from OpenFrameworks +layout(location = 0) in vec4 vertexPosition_ModelSpace; + +uniform vec4 Color; +uniform mat4 ProjectionMatrix; +uniform mat4 ModelMatrix; +uniform mat4 ViewMatrix; + +void main() +{ + gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix * vertexPosition_ModelSpace; +} \ No newline at end of file diff --git a/example/bin/data/shaders/neighbor_shader.frag b/example/bin/data/shaders/neighbor_shader.frag new file mode 100644 index 0000000..c789db6 --- /dev/null +++ b/example/bin/data/shaders/neighbor_shader.frag @@ -0,0 +1,9 @@ +#version 330 + +uniform vec4 Color; +out vec4 outputColor; + +void main() +{ + outputColor = Color; +} \ No newline at end of file diff --git a/example/bin/data/shaders/neighbor_shader.vert b/example/bin/data/shaders/neighbor_shader.vert new file mode 100644 index 0000000..c1910b8 --- /dev/null +++ b/example/bin/data/shaders/neighbor_shader.vert @@ -0,0 +1,15 @@ +#version 330 + +// these are for the programmable pipeline system and are passed in +// by default from OpenFrameworks +layout(location = 0) in vec4 vertexPosition_ModelSpace; + +uniform vec4 Color; +uniform mat4 ProjectionMatrix; +uniform mat4 ModelMatrix; +uniform mat4 ViewMatrix; + +void main() +{ + gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix * vertexPosition_ModelSpace; +} \ No newline at end of file diff --git a/example/bin/data/shaders/trail_shader.frag b/example/bin/data/shaders/trail_shader.frag new file mode 100644 index 0000000..8bd1308 --- /dev/null +++ b/example/bin/data/shaders/trail_shader.frag @@ -0,0 +1,11 @@ +#version 330 + +in float v_alpha; +uniform vec4 Color; +out vec4 outputColor; + +void main() +{ + outputColor = Color; + outputColor.a *= v_alpha; +} \ No newline at end of file diff --git a/example/bin/data/shaders/trail_shader.vert b/example/bin/data/shaders/trail_shader.vert new file mode 100644 index 0000000..70b2433 --- /dev/null +++ b/example/bin/data/shaders/trail_shader.vert @@ -0,0 +1,18 @@ +#version 330 + +// these are for the programmable pipeline system and are passed in +// by default from OpenFrameworks +layout(location = 0) in vec4 vertexPosition_ModelSpace; +layout (location = 1) in float alpha; +out float v_alpha; + +uniform vec4 Color; +uniform mat4 ProjectionMatrix; +uniform mat4 ModelMatrix; +uniform mat4 ViewMatrix; + +void main() +{ + v_alpha = alpha; + gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix * vertexPosition_ModelSpace; +} \ No newline at end of file diff --git a/example/config.make b/example/config.make new file mode 100644 index 0000000..d725b97 --- /dev/null +++ b/example/config.make @@ -0,0 +1,140 @@ +################################################################################ +# CONFIGURE PROJECT MAKEFILE (optional) +# This file is where we make project specific configurations. +################################################################################ + +################################################################################ +# OF ROOT +# The location of your root openFrameworks installation +# (default) OF_ROOT = ../../.. +################################################################################ +OF_ROOT = C:\Users\dbisig\Programming\of_v0.11.2 + +################################################################################ +# PROJECT ROOT +# The location of the project - a starting place for searching for files +# (default) PROJECT_ROOT = . (this directory) +# +################################################################################ +# PROJECT_ROOT = . + +################################################################################ +# PROJECT SPECIFIC CHECKS +# This is a project defined section to create internal makefile flags to +# conditionally enable or disable the addition of various features within +# this makefile. For instance, if you want to make changes based on whether +# GTK is installed, one might test that here and create a variable to check. +################################################################################ +# None + +################################################################################ +# PROJECT EXTERNAL SOURCE PATHS +# These are fully qualified paths that are not within the PROJECT_ROOT folder. +# Like source folders in the PROJECT_ROOT, these paths are subject to +# exlclusion via the PROJECT_EXLCUSIONS list. +# +# (default) PROJECT_EXTERNAL_SOURCE_PATHS = (blank) +# +# Note: Leave a leading space when adding list items with the += operator +################################################################################ +# PROJECT_EXTERNAL_SOURCE_PATHS = + +################################################################################ +# PROJECT EXCLUSIONS +# These makefiles assume that all folders in your current project directory +# and any listed in the PROJECT_EXTERNAL_SOURCH_PATHS are are valid locations +# to look for source code. The any folders or files that match any of the +# items in the PROJECT_EXCLUSIONS list below will be ignored. +# +# Each item in the PROJECT_EXCLUSIONS list will be treated as a complete +# string unless teh user adds a wildcard (%) operator to match subdirectories. +# GNU make only allows one wildcard for matching. The second wildcard (%) is +# treated literally. +# +# (default) PROJECT_EXCLUSIONS = (blank) +# +# Will automatically exclude the following: +# +# $(PROJECT_ROOT)/bin% +# $(PROJECT_ROOT)/obj% +# $(PROJECT_ROOT)/%.xcodeproj +# +# Note: Leave a leading space when adding list items with the += operator +################################################################################ +# PROJECT_EXCLUSIONS = + +################################################################################ +# PROJECT LINKER FLAGS +# These flags will be sent to the linker when compiling the executable. +# +# (default) PROJECT_LDFLAGS = -Wl,-rpath=./libs +# +# Note: Leave a leading space when adding list items with the += operator +# +# Currently, shared libraries that are needed are copied to the +# $(PROJECT_ROOT)/bin/libs directory. The following LDFLAGS tell the linker to +# add a runtime path to search for those shared libraries, since they aren't +# incorporated directly into the final executable application binary. +################################################################################ +# PROJECT_LDFLAGS=-Wl,-rpath=./libs +################################################################################ +# PROJECT DEFINES +# Create a space-delimited list of DEFINES. The list will be converted into +# CFLAGS with the "-D" flag later in the makefile. +# +# (default) PROJECT_DEFINES = (blank) +# +# Note: Leave a leading space when adding list items with the += operator +################################################################################ +# PROJECT_DEFINES = + +################################################################################ +# PROJECT CFLAGS +# This is a list of fully qualified CFLAGS required when compiling for this +# project. These CFLAGS will be used IN ADDITION TO the PLATFORM_CFLAGS +# defined in your platform specific core configuration files. These flags are +# presented to the compiler BEFORE the PROJECT_OPTIMIZATION_CFLAGS below. +# +# (default) PROJECT_CFLAGS = (blank) +# +# Note: Before adding PROJECT_CFLAGS, note that the PLATFORM_CFLAGS defined in +# your platform specific configuration file will be applied by default and +# further flags here may not be needed. +# +# Note: Leave a leading space when adding list items with the += operator +################################################################################ +# PROJECT_CFLAGS = + +################################################################################ +# PROJECT OPTIMIZATION CFLAGS +# These are lists of CFLAGS that are target-specific. While any flags could +# be conditionally added, they are usually limited to optimization flags. +# These flags are added BEFORE the PROJECT_CFLAGS. +# +# PROJECT_OPTIMIZATION_CFLAGS_RELEASE flags are only applied to RELEASE targets. +# +# (default) PROJECT_OPTIMIZATION_CFLAGS_RELEASE = (blank) +# +# PROJECT_OPTIMIZATION_CFLAGS_DEBUG flags are only applied to DEBUG targets. +# +# (default) PROJECT_OPTIMIZATION_CFLAGS_DEBUG = (blank) +# +# Note: Before adding PROJECT_OPTIMIZATION_CFLAGS, please note that the +# PLATFORM_OPTIMIZATION_CFLAGS defined in your platform specific configuration +# file will be applied by default and further optimization flags here may not +# be needed. +# +# Note: Leave a leading space when adding list items with the += operator +################################################################################ +# PROJECT_OPTIMIZATION_CFLAGS_RELEASE = +# PROJECT_OPTIMIZATION_CFLAGS_DEBUG = + +################################################################################ +# PROJECT COMPILERS +# Custom compilers can be set for CC and CXX +# (default) PROJECT_CXX = (blank) +# (default) PROJECT_CC = (blank) +# Note: Leave a leading space when adding list items with the += operator +################################################################################ +# PROJECT_CXX = +# PROJECT_CC = diff --git a/example/src/main.cpp b/example/src/main.cpp new file mode 100644 index 0000000..81ab23d --- /dev/null +++ b/example/src/main.cpp @@ -0,0 +1,16 @@ +#include "ofMain.h" +#include "ofApp.h" + +//======================================================================== +int main() +{ + ofGLWindowSettings settings; + settings.setGLVersion(3, 2); + ofCreateWindow(settings); + + // this kicks off the running of my app + // can be OF_WINDOW or OF_FULLSCREEN + // pass in width and height too: + ofRunApp(new ofApp()); + +} diff --git a/example/src/ofApp.cpp b/example/src/ofApp.cpp new file mode 100644 index 0000000..0999c9c --- /dev/null +++ b/example/src/ofApp.cpp @@ -0,0 +1,401 @@ +#include "ofApp.h" +#include "dab_flock_parameter.h" +#include "dab_flock_euler_integration.h" +#include "dab_flock_agent.h" +#include "dab_flock_swarm.h" +#include "dab_flock_simulation.h" +#include "dab_flock_com.h" +#include "dab_flock_osc_control.h" +#include "dab_space_includes.h" +#include "dab_flock_behavior_includes.h" +#include "dab_flock_visual.h" +#include "dab_flock_com.h" +#include "dab_math_roesseler_field_algorithm.h" +#include "dab_geom_line.h" +#include "ofTrueTypeFont.h" +#include "dab_flock_text_tools.h" +#include "dab_flock_serialize.h" +using namespace dab; +using namespace dab::flock; + +//-------------------------------------------------------------- +void ofApp::setup() +{ + //ofTrueTypeFont testFont; + //testFont.load("Batang.ttf", 160, true, true, true); + //std::string string = "A"; + + //std::vector textChars = testFont.getStringAsPoints(string, false, false); + + //std::cout << "textCharCount " << textChars.size() << "\n"; + //for (int cI = 0; cI < textChars.size(); ++cI) + //{ + // ofPath& testCharContour = textChars[cI]; + + // std::vector pathCommands = testCharContour.getCommands(); + // std::cout << "pathCommands " << pathCommands.size() << "\n"; + // const std::vector& charOutlines = testCharContour.getOutline(); + // int outlineCount = charOutlines.size(); + // std::cout << "outlineCount " << outlineCount << "\n"; + // for (int oI = 0; oI < outlineCount; ++oI) + // { + // std::cout << "oI " << oI << " s " << charOutlines[oI].size() << "\n"; + // } + //} + + //char letter = '$'; + //bool vflip = true; + //bool filled = false; + //ofPath testCharContour = testFont.getCharacterAsPoints(letter, vflip, filled); + + //ofPath testCharContour = testFont.getCharacterAsPoints(letter, vflip, filled); + + //std::vector pathCommands = testCharContour.getCommands(); + //std::cout << "pathCommands " << pathCommands.size() << "\n"; + //const std::vector& charOutlines = testCharContour.getOutline(); + //int outlineCount = charOutlines.size(); + //std::cout << "outlineCount " << outlineCount << "\n"; + //for (int oI = 0; oI < outlineCount; ++oI) + //{ + // std::cout << "oI " << oI << " s " << charOutlines[oI].size() << "\n"; + //} + + + SerializeTools& serializeTools = SerializeTools::get(); + + // // debug + // ofTrueTypeFont font; + // bool success; + // success = font.load("/Library/Fonts/InaiMathi.ttf", 2, true, true, true, 0.3, 0); + // font.setLineHeight( font.getLineHeight() * 2.0 ); + // font.setLetterSpacing(font.getLetterSpacing() * 2.0); + // font.setSpaceSize(20.0); + // + // + // std::cout << "success " << success << "\n"; + // + // std::vector ttfChars = font.getStringAsPoints("h h\nh h"); + // int charCount = ttfChars.size(); + // + // std::cout << "charCount " << charCount << "\n"; + // + // ofVec3f textMinPos(FLT_MAX, FLT_MAX, FLT_MAX); + // ofVec3f textMaxPos(-FLT_MIN, -FLT_MIN, -FLT_MIN); + // ofVec3f charMinPos(FLT_MAX, FLT_MAX, FLT_MAX); + // ofVec3f charMaxPos(-FLT_MIN, -FLT_MIN, -FLT_MIN); + // + // for(int cI=0; cI& ttfOulines = ttfChar.getOutline(); + // + // int outlineCount = ttfOulines.size(); + // + // std::cout << "cI " << cI << " outlineCount " << outlineCount << "\n"; + // + // ofVec3f charMinPos = ofVec3f(FLT_MAX, FLT_MAX, FLT_MAX); + // ofVec3f charMaxPos = ofVec3f(-FLT_MIN, -FLT_MIN, -FLT_MIN); + // + // for(int oI=0; oI textMaxPos[0]) textMaxPos[0] = point.x; + // if(point.y > textMaxPos[1]) textMaxPos[1] = point.y; + // if(point.z > textMaxPos[2]) textMaxPos[2] = point.z; + // + // if(point.x < charMinPos[0]) charMinPos[0] = point.x; + // if(point.y < charMinPos[1]) charMinPos[1] = point.y; + // if(point.z < charMinPos[2]) charMinPos[2] = point.z; + // if(point.x > charMaxPos[0]) charMaxPos[0] = point.x; + // if(point.y > charMaxPos[1]) charMaxPos[1] = point.y; + // if(point.z > charMaxPos[2]) charMaxPos[2] = point.z; + // } + // } + // + // std::cout << "char " << cI << " Min " << charMinPos[0] << " " << charMinPos[1] << " " << charMinPos[2] << "\n"; + // std::cout << "char " << cI << " Max " << charMaxPos[0] << " " << charMaxPos[1] << " " << charMaxPos[2] << "\n"; + // } + // + // std::cout << "text Min " << textMinPos[0] << " " << textMinPos[1] << " " << textMinPos[2] << "\n"; + // std::cout << "text Max " << textMaxPos[0] << " " << textMaxPos[1] << " " << textMaxPos[2] << "\n"; + // + // std::exit(0); + // + // // debug done + + try + { + Simulation& simulation = Simulation::get(); + simulation.setUpdateInterval(1.0); + + simulation.com().createOscControl(7400, "127.0.0.1", 7800); + //simulation.com().createSender("FlockSender", "127.0.0.1", 7500, false); + simulation.com().createSender("FlockSender", "127.0.0.1", 7500, true); + + // create agent space + //simulation.space().addSpace(std::shared_ptr(new space::Space("agent_position", new space::ANNAlg(3)))); + simulation.space().addSpace(std::shared_ptr(new space::Space("agent_position", new space::KDTreeAlg(3)))); + //simulation.space().addSpace(std::shared_ptr(new space::Space("agent_position", new space::NTreeAlg(3)))); + + // create grid space + space::SpaceGrid* forceGrid = new space::SpaceGrid(3, Array{ 20, 20, 5 }, Eigen::Vector3f(-5.0, -5.0, -5.0), Eigen::Vector3f(5.0, 5.0, 5.0)); + simulation.space().addSpace(std::shared_ptr(new space::Space("forcegrid", new space::GridAlg(forceGrid, space::GridAlg::AvgLocationMode, space::GridAlg::NoUpdateMode)))); + + // create shape space and line object + space::RTreeAlg* shapeSpaceAlg = new space::RTreeAlg(Eigen::Vector3f(-5.0, -5.0, -5.0), Eigen::Vector3f(5.0, 5.0, 5.0)); + //shapeSpaceAlg->setClosestPointType(space::ClosestPointShape); + simulation.space().addSpace(std::shared_ptr(new space::Space("shapespace", shapeSpaceAlg))); + std::shared_ptr lineGeomGroup(new geom::GeometryGroup()); + lineGeomGroup->addGeometry(new geom::Line(glm::vec3(-1.0, -1.0, 0.0), glm::vec3(1.0, -1.0, 0.0))); + lineGeomGroup->addGeometry(new geom::Line(glm::vec3(1.0, -1.0, 0.0), glm::vec3(0.0, 1.0, 0.0))); + lineGeomGroup->addGeometry(new geom::Line(glm::vec3(0.0, 1.0, 0.0), glm::vec3(-1.0, -1.0, 0.0))); + // lineGeomGroup->addGeometry(new geom::Line(glm::vec3(0.1875, -0.1875, 0.0), glm::vec3( 0.1875, 0.0, 0.0))); + // lineGeomGroup->addGeometry(new geom::Line(glm::vec3(0.1875, 0.0, 0.0), glm::vec3( 0.09375, 0.0, 0.0))); + // lineGeomGroup->addGeometry(new geom::Line(glm::vec3(0.09375, 0.0, 0.0), glm::vec3( 0.09375, -0.875, 0.0))); + // lineGeomGroup->addGeometry(new geom::Line(glm::vec3(0.09375, -0.875, 0.0), glm::vec3( 0.1875, -0.1875, 0.0))); + space::SpaceShape* lineShape = new space::SpaceShape(lineGeomGroup); + simulation.space().addObject("shapespace", lineShape, true); + + // todo: getting text outlines doesn't work + // create text space and text object + space::RTreeAlg* textSpaceAlg = new space::RTreeAlg(Eigen::Vector3f(-5.0, -5.0, -5.0), Eigen::Vector3f(5.0, 5.0, 5.0)); + simulation.space().addSpace(std::shared_ptr(new space::Space("textspace", textSpaceAlg))); + TextTools& textTools = TextTools::get(); + textTools.createFont("defaultfont", "C:/Windows/Fonts/Ebrima.ttf"); + textTools.createText("char0", "A", "defaultfont"); + space::SpaceShape* textShape = textTools.text("char0"); + textShape->setScale(Eigen::Vector3f(5.0, 5.0, 5.0)); + textShape->setPosition(Eigen::Vector3f(0.0, 2.0, 0.0)); + simulation.space().addObject("textspace", textShape, true); + + math::VectorField& vectorField = forceGrid->vectorField(); + math::RoesselerFieldAlgorithm fieldAlg; + fieldAlg.setVectorField(&vectorField); + fieldAlg.update(); + + Swarm* swarm = new Swarm("swarm"); + swarm->addParameter("position", { 0.0, 0.0, 0.0 }); + swarm->assignNeighbors("position", "agent_position", true, new space::NeighborGroupAlg(3.0, 8, true)); + swarm->assignNeighbors("position", "forcegrid", false, new space::NeighborGroupAlg(2.5, 1, true)); + swarm->assignNeighbors("position", "shapespace", false, new space::NeighborGroupAlg(60.0, 1, true)); + swarm->assignNeighbors("position", "textspace", false, new space::NeighborGroupAlg(60.0, 1, true)); + swarm->addParameter("velocity", { 0.0, 0.0, 0.0 }); + swarm->addParameter("acceleration", { 0.0, 0.0, 0.0 }); + swarm->addParameter("force", { 0.0, 0.0, 0.0 }); + swarm->addParameter("mass", { 0.1f }); + + swarm->addBehavior("resetForce", ResetBehavior("", "force")); + + swarm->addBehavior("randomize", RandomizeBehavior("", "force")); + swarm->set("randomize_range", { 0.0001f, 0.0001f, 0.0001f }); + + swarm->addBehavior("damping", DampingBehavior("velocity", "force")); + swarm->set("damping_prefVelocity", { 0.5 }); + swarm->set("damping_amount", { 0.1f }); + + swarm->addBehavior("targetPos", TargetParameterBehavior("position", "force")); + swarm->set("targetPos_target", { 0.0, 0.0, 0.0 }); + swarm->set("targetPos_adapt", { 1.0, 1.0, 1.0 }); + swarm->set("targetPos_amount", 0.0); // 0.52 // 0.0 + swarm->set("targetPos_absolute", 0.0); + + swarm->addBehavior("targetVel", TargetParameterBehavior("velocity", "force")); + swarm->set("targetVel_target", { 1.0, 0.0, 0.0 }); + swarm->set("targetVel_adapt", { 1.0, 1.0, 1.0 }); + swarm->set("targetVel_amount", 0.0); // 0.52 // 0.0 + swarm->set("targetVel_absolute", 0.0); + + swarm->addBehavior("circular", CircularBehavior("position velocity", "force")); + swarm->set("circular_centerPosition", { 0.0, 0.0, 0.0 }); + swarm->set("circular_innerRadius", 2.0); + swarm->set("circular_outerRadius", 2.0); + swarm->set("circular_ortAmount", 0.0); + swarm->set("circular_tanAmount", 0.0); + + //swarm->addBehavior("shapeFollow", LineFollowBehavior("position@shapespace", "force")); + //swarm->set("shapeFollow_minDist", 0.0); + //swarm->set("shapeFollow_maxDist", 60.0); + //swarm->set("shapeFollow_contourMaintainDist", 5.0); + //swarm->set("shapeFollow_tanAmount", 0.02); + //swarm->set("shapeFollow_ortAmount", 2.0); + //swarm->set("shapeFollow_amount", 1.0); + + swarm->addBehavior( "textFollow", LineFollowBehavior("position@textspace","force")); + swarm->set("textFollow_minDist", 0.0); + swarm->set("textFollow_maxDist", 60.0); + swarm->set("textFollow_contourMaintainDist", 5.0); + swarm->set("textFollow_tanAmount", 0.02); + swarm->set("textFollow_ortAmount", 2.0); + swarm->set("textFollow_amount", 1.0); + + //swarm->addBehavior( "forcegrid", GridAvgBehavior( "position@forcegrid", "force" ) ); + //swarm->set("forcegrid_amount", { 0.01 } ); + + //swarm->addBehavior("cohesion", CohesionBehavior("position@agent_position", "force")); + //swarm->set("cohesion_minDist", { 0.0 }); + //swarm->set("cohesion_maxDist", { 3.0 }); + //swarm->set("cohesion_amount", { 0.05f }); + + //swarm->addBehavior("alignment", AlignmentBehavior("position@agent_position:velocity", "force")); + //swarm->set("alignment_minDist", { 0.0 }); + //swarm->set("alignment_maxDist", { 3.0 }); + //swarm->set("alignment_amount", { 0.05f }); + + //swarm->addBehavior("evasion", EvasionBehavior("position@agent_position", "force")); + //swarm->set("evasion_maxDist", { 0.2f }); + //swarm->set("evasion_amount", { 0.2f }); + + swarm->addBehavior("boundaryWrap", BoundaryWrapBehavior("position", "position")); + swarm->set("boundaryWrap_lowerBoundary", { -5.0, -5.0, -5.0 }); + swarm->set("boundaryWrap_upperBoundary", { 5.0, 5.0, 5.0 }); + + // swarm->addBehavior( "boundaryRepulsion", BoundaryRepulsionBehavior( "position", "force" ) ); + // swarm->set( "boundaryRepulsion_lowerBoundary", { -5.0, -5.0, -5.0 } ); + // swarm->set( "boundaryRepulsion_upperBoundary", { 5.0, 5.0, 5.0 } ); + // swarm->set( "boundaryRepulsion_maxDist", { 2.5 } ); + // swarm->set( "boundaryRepulsion_amount", { 0.13 } ); + + swarm->addBehavior("boundaryMirror", BoundaryMirrorBehavior("position velocity force", "velocity force")); + swarm->set("boundaryMirror_lowerBoundary", { -5.0, -5.0, -5.0 }); + swarm->set("boundaryMirror_upperBoundary", { 5.0, 5.0, 5.0 }); + swarm->set("boundaryMirror_active", 0.0); + + swarm->addBehavior("acceleration", AccelerationBehavior("mass velocity force", "acceleration")); + swarm->set("acceleration_maxAngularAcceleration", { 1.0, 1.0, 1.0 }); + + swarm->addBehavior("integration", EulerIntegration("position velocity acceleration", "position velocity")); + swarm->set("integration_timestep", { 0.1f }); + + //swarm->addBehavior( "print", ParameterPrintBehavior("position", "") ); + + swarm->addAgents(100); + swarm->randomize("position", { -1.0, -1.0, -1.0 }, { 1.0, 1.0, 1.0 }); + + //std::cout << "swarm\n" << swarm->info(2) << "\n"; + + //simulation.com().registerParameter("FlockSender", "swarm", "position" ); + //simulation.com().registerParameter("FlockSender", "swarm", "position", Eigen::Vector3f(-5.0, -5.0, -5.0), Eigen::Vector3f(5.0, 5.0, 5.0) ); + //simulation.com().registerParameter("FlockSender", "swarm", "position", std::array( { 5, 10 } ) ); + //simulation.com().registerParameter("FlockSender", "swarm", "position", swarm->agentCount() ); + simulation.com().registerParameter("FlockSender", "swarm", "position", swarm->agentCount(), Eigen::Vector3f(-5.0, -5.0, -5.0), Eigen::Vector3f(5.0, 5.0, 5.0)); + //simulation.com().registerParameter("FlockSender", "swarm", "velocity", swarm->agentCount()); + + FlockVisuals& visuals = FlockVisuals::get(); + visuals.showSwarm("swarm", "position", "velocity", 10); + + //visuals.showSwarm("swarm", "position", ""); + visuals.setAgentScale("swarm", 0.05); + // visuals.showSpace("agent_position"); + // visuals.showSpace("forcegrid"); + // visuals.setSpaceColor("forcegrid", std::array( { 0.0, 0.0, 0.0, 0.2 } )); + // visuals.setSpaceValueScale("forcegrid", 0.1); + + simulation.start(); + simulation.setUpdateInterval(20.0); + } + catch (dab::Exception& e) + { + std::cout << e << "\n"; + } + + ofSetFrameRate(60); +} + +//-------------------------------------------------------------- +void ofApp::update() +{ + //std::cout << "ofApp::update() begin\n"; + + //Simulation::get().update(); + FlockVisuals::get().update(); + + //std::cout << "ofApp::update() end\n"; +} + +//-------------------------------------------------------------- +void ofApp::draw() +{ + //std::cout << "ofApp::draw() begin\n"; + + //FlockVisuals::get().update(); + FlockVisuals::get().display(); + + //std::cout << "ofApp::draw() end\n"; +} + +//-------------------------------------------------------------- +void ofApp::keyPressed(int key) +{ + if (key == 'f') + { + ofGetMainLoop()->getCurrentWindow()->toggleFullscreen(); + //ofGetMainLoop()->getCurrentWindow()->setWindowShape(windowSize[0], windowSize[1]); + //ofGetMainLoop()->getCurrentWindow()->setWindowPosition(windowPos[0], windowPos[1]); + } +} + +//-------------------------------------------------------------- +void ofApp::keyReleased(int key) { + +} + +//-------------------------------------------------------------- +void ofApp::mouseMoved(int x, int y) { + +} + +//-------------------------------------------------------------- +void ofApp::mouseDragged(int x, int y, int button) { + +} + +//-------------------------------------------------------------- +void ofApp::mousePressed(int x, int y, int button) { + +} + +//-------------------------------------------------------------- +void ofApp::mouseReleased(int x, int y, int button) { + +} + +//-------------------------------------------------------------- +void ofApp::mouseEntered(int x, int y) { + +} + +//-------------------------------------------------------------- +void ofApp::mouseExited(int x, int y) { + +} + +//-------------------------------------------------------------- +void ofApp::windowResized(int w, int h) { + +} + +//-------------------------------------------------------------- +void ofApp::gotMessage(ofMessage msg) { + +} + +//-------------------------------------------------------------- +void ofApp::dragEvent(ofDragInfo dragInfo) { + +} + diff --git a/example/src/ofApp.h b/example/src/ofApp.h new file mode 100644 index 0000000..a9a1eea --- /dev/null +++ b/example/src/ofApp.h @@ -0,0 +1,24 @@ +#pragma once + +#include "ofMain.h" + +class ofApp : public ofBaseApp{ + + public: + void setup(); + void update(); + void draw(); + + void keyPressed(int key); + void keyReleased(int key); + void mouseMoved(int x, int y ); + void mouseDragged(int x, int y, int button); + void mousePressed(int x, int y, int button); + void mouseReleased(int x, int y, int button); + void mouseEntered(int x, int y); + void mouseExited(int x, int y); + void windowResized(int w, int h); + void dragEvent(ofDragInfo dragInfo); + void gotMessage(ofMessage msg); + +};