[168] | 1 | /* |
---|
| 2 | Bullet Continuous Collision Detection and Physics Library |
---|
| 3 | Copyright (c) 2003-2007 Erwin Coumans http://continuousphysics.com/Bullet/ |
---|
| 4 | |
---|
| 5 | This software is provided 'as-is', without any express or implied warranty. |
---|
| 6 | In no event will the authors be held liable for any damages arising from the use of this software. |
---|
| 7 | Permission is granted to anyone to use this software for any purpose, |
---|
| 8 | including commercial applications, and to alter it and redistribute it freely, |
---|
| 9 | subject to the following restrictions: |
---|
| 10 | |
---|
| 11 | 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. |
---|
| 12 | 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. |
---|
| 13 | 3. This notice may not be removed or altered from any source distribution. |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | #include "BasicDemo.h" |
---|
| 17 | #include "GLDebugDrawer.h" |
---|
| 18 | #include "btBulletDynamicsCommon.h" |
---|
| 19 | #include "LinearMath/btHashMap.h" |
---|
| 20 | #include <mpi.h> |
---|
| 21 | #include <stdio.h> |
---|
| 22 | #include <gcj/cni.h> |
---|
| 23 | #include <signal.h> |
---|
| 24 | #include <java/lang/System.h> |
---|
| 25 | #include <java/io/PrintStream.h> |
---|
| 26 | #include <java/util/List.h> |
---|
| 27 | #include <java/util/ArrayList.h> |
---|
| 28 | #include <java/lang/System.h> |
---|
| 29 | #include <java/io/PrintStream.h> |
---|
| 30 | #include <java/lang/Throwable.h> |
---|
| 31 | #include "Agent.h" |
---|
| 32 | #include "Sensor.h" |
---|
| 33 | |
---|
| 34 | #define WIDTH 10 |
---|
| 35 | #define HEIGHT 3 |
---|
| 36 | #define MAXPROCS (WIDTH * HEIGHT) |
---|
| 37 | |
---|
| 38 | BasicDemo vworld; |
---|
| 39 | GLDebugDrawer gDebugDrawer; |
---|
| 40 | btDynamicsWorld* m_dynamicWorld; |
---|
| 41 | |
---|
| 42 | using namespace java::lang; |
---|
| 43 | using namespace java::util; |
---|
| 44 | |
---|
| 45 | enum { |
---|
| 46 | LEFT=0, RIGHT=1, UP=2, DOWN=3 |
---|
| 47 | }; |
---|
| 48 | |
---|
| 49 | int rank, nprocs = MAXPROCS; |
---|
| 50 | MPI_Comm everyone; |
---|
| 51 | |
---|
| 52 | void init() { |
---|
| 53 | if(JvCreateJavaVM(NULL) < 0) { |
---|
| 54 | printf("Error creating the JVM!!!\n"); |
---|
| 55 | exit(1); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | JvAttachCurrentThread(NULL, NULL); |
---|
| 59 | |
---|
| 60 | JvInitClass(&Agent::class$); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | /* |
---|
| 64 | int getRight(int i, int j) { |
---|
| 65 | return (4 * HEIGHT - 2) * i + 4 * j + 0; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | int getLeft(int i, int j) { |
---|
| 69 | return getRight(i-1, j) + 1; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | int getDown(int i, int j) { |
---|
| 73 | int down = (4 * HEIGHT - 2) * i + 4 * j + 2; |
---|
| 74 | if(i == WIDTH - 1) |
---|
| 75 | down -= 2 + j * 2; |
---|
| 76 | |
---|
| 77 | return down; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | int getUp(int i, int j) { |
---|
| 81 | int up = getDown(i, j-1) + 1; |
---|
| 82 | return up; |
---|
| 83 | } |
---|
| 84 | */ |
---|
| 85 | |
---|
| 86 | static void master() { |
---|
| 87 | int i, j; |
---|
| 88 | printf("Master process here!\n"); |
---|
| 89 | |
---|
| 90 | /// Initialize the scene of the simulation |
---|
| 91 | vworld.initPhysics(); |
---|
| 92 | vworld.createSimulation2D(WIDTH, HEIGHT); |
---|
| 93 | // vworld.createSimulation3D(3, 3, 3); |
---|
| 94 | vworld.getDynamicsWorld()->setDebugDrawer(&gDebugDrawer); |
---|
| 95 | |
---|
| 96 | m_dynamicWorld = vworld.getDynamicsWorld(); |
---|
| 97 | |
---|
| 98 | init(); |
---|
| 99 | |
---|
| 100 | for(i = 0; i < WIDTH; i++) { |
---|
| 101 | for(j = 0; j < HEIGHT; j++) { |
---|
| 102 | List *sensors = (List *) new ArrayList(); |
---|
| 103 | // int right = getRight(i, j); |
---|
| 104 | // int left = getLeft(i, j); |
---|
| 105 | // int up = getUp(i, j); |
---|
| 106 | // int down = getDown(i, j); |
---|
| 107 | int idx; |
---|
| 108 | |
---|
| 109 | if(i < WIDTH - 1) { |
---|
| 110 | idx = vworld.m_matrix[i * HEIGHT + j][RIGHT]; |
---|
| 111 | // printf("RIGHT: (%d, %d) - %d\n", i, j, idx); |
---|
| 112 | btHingeConstraint *joint = (btHingeConstraint*)vworld.m_joints[idx]; |
---|
| 113 | Sensor *s = new Sensor(joint->getHingeAngle(),joint->getLowerLimit(),joint->getUpperLimit(), RIGHT); |
---|
| 114 | sensors->add(s); |
---|
| 115 | vworld.m_sensors.push_back(make_pair(idx, s)); |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | if(i > 0) { |
---|
| 119 | idx = vworld.m_matrix[i * HEIGHT + j][LEFT]; |
---|
| 120 | // printf("LEFT: (%d, %d) - %d\n", i, j, idx); |
---|
| 121 | btHingeConstraint *joint = (btHingeConstraint*)vworld.m_joints[idx]; |
---|
| 122 | Sensor *s = new Sensor(joint->getHingeAngle(),joint->getLowerLimit(),joint->getUpperLimit(), LEFT); |
---|
| 123 | sensors->add(s); |
---|
| 124 | vworld.m_sensors.push_back(make_pair(idx, s)); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | if(j < HEIGHT - 1) { |
---|
| 128 | idx = vworld.m_matrix[i * HEIGHT + j][DOWN]; |
---|
| 129 | // printf("DOWN: (%d, %d) - %d\n", i, j, idx); |
---|
| 130 | btHingeConstraint *joint = (btHingeConstraint*)vworld.m_joints[idx]; |
---|
| 131 | Sensor *s = new Sensor(joint->getHingeAngle(),joint->getLowerLimit(),joint->getUpperLimit(), DOWN); |
---|
| 132 | sensors->add(s); |
---|
| 133 | vworld.m_sensors.push_back(make_pair(idx, s)); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | if(j > 0) { |
---|
| 137 | idx = vworld.m_matrix[i * HEIGHT + j][UP]; |
---|
| 138 | // printf("UP: (%d, %d) - %d\n", i, j, idx); |
---|
| 139 | btHingeConstraint *joint = (btHingeConstraint*)vworld.m_joints[idx]; |
---|
| 140 | Sensor *s = new Sensor(joint->getHingeAngle(),joint->getLowerLimit(),joint->getUpperLimit(), UP); |
---|
| 141 | sensors->add(s); |
---|
| 142 | vworld.m_sensors.push_back(make_pair(idx, s)); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | Agent *agent = new Agent(sensors, i, j); |
---|
| 146 | vworld.m_agents.push_back(agent); |
---|
| 147 | } |
---|
| 148 | } |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | |
---|
| 152 | static void slave() { |
---|
| 153 | |
---|
| 154 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
---|
| 155 | printf("Process slave number %d executing...\n", rank); |
---|
| 156 | |
---|
| 157 | while(1) { |
---|
| 158 | sleep(1000); |
---|
| 159 | } |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | int main(int argc,char** argv) { |
---|
| 164 | |
---|
| 165 | MPI_Init(&argc, &argv); |
---|
| 166 | |
---|
| 167 | MPI_Comm_size(MPI_COMM_WORLD, &nprocs); |
---|
| 168 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
---|
| 169 | |
---|
| 170 | printf("%d\n", rank); |
---|
| 171 | |
---|
| 172 | if(nprocs != MAXPROCS + 1) { |
---|
| 173 | // return 0; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | if(rank == 0) { |
---|
| 177 | master(); |
---|
| 178 | } else { |
---|
| 179 | slave(); |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | if(rank == 0) { |
---|
| 183 | #ifdef CHECK_MEMORY_LEAKS |
---|
| 184 | vworld.exitPhysics(); |
---|
| 185 | #else |
---|
| 186 | int ret; |
---|
| 187 | try { |
---|
| 188 | ret = glutmain(argc, argv, 640, 480, "SIMEO", &vworld); |
---|
| 189 | } catch (Throwable *t) |
---|
| 190 | { |
---|
| 191 | System::err->println(JvNewStringLatin1("Unhandled Java exception:")); |
---|
| 192 | t->printStackTrace(); |
---|
| 193 | } |
---|
| 194 | MPI_Finalize(); |
---|
| 195 | return ret; |
---|
| 196 | #endif |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | //default glut doesn't return from mainloop |
---|
| 200 | return 0; |
---|
| 201 | } |
---|