示例#1
0
 private Method findCallback(final String name) {
   try {
     return parent.getClass().getMethod(name, this.getClass());
   } catch (Exception e) {
   }
   // Permit callback(Object) as alternative to callback(Serial).
   try {
     return parent.getClass().getMethod(name, Object.class);
   } catch (Exception e) {
   }
   return null;
 }
示例#2
0
 /**
  * a Constructor, usually called in the setup() method in your sketch to initialize and start the
  * library.
  *
  * @example portaTest
  * @param p
  */
 public PortaMod(PApplet p) {
   this.p = p;
   IBXM.SetMC(this);
   try {
     noteArrived = p.getClass().getMethod("grabNewdata", new Class[] {PortaMod.class});
   } catch (Exception e) {
     PApplet.println(e.getMessage());
   }
   try {
     noteArrivedB = p.getClass().getMethod("grabNewdataB", new Class[] {PortaMod.class});
   } catch (Exception e) {
     PApplet.println(e.getMessage());
   }
 }
示例#3
0
  /**
   * @param parent typically use "this"
   * @param port port used to transfer data
   * @param host when multiple NICs are in use, the ip (or name) to bind from
   */
  public MyServer(PApplet parent, int port, String host) {
    this.parent = parent;
    this.port = port;

    try {
      if (host == null) {
        server = new ServerSocket(this.port);
      } else {
        server = new ServerSocket(this.port, 10, InetAddress.getByName(host));
      }
      // clients = new Vector();
      clients = new Client[10];

      thread = new Thread(this);
      thread.start();

      parent.registerMethod("dispose", this);

      // reflection to check whether host applet has a call for
      // public void serverEvent(MyServer s, Client c);
      // which is called when a new guy connects
      try {
        serverEventMethod =
            parent.getClass().getMethod("serverEvent", new Class[] {MyServer.class, Client.class});
      } catch (Exception e) {
        // no such method, or an error.. which is fine, just ignore
        serverEventMethod = null;
      }

      try {
        clientValidationMethod =
            parent
                .getClass()
                .getMethod("clientValidation", new Class[] {MyServer.class, Client.class});
      } catch (Exception e) {
        // no such method, or an error.. which is fine, just ignore
        clientValidationMethod = null;
      }

    } catch (IOException e) {
      // e.printStackTrace();
      thread = null;
      throw new RuntimeException(e);
      // errorMessage("<init>", e);
    }
  }
示例#4
0
  public XBeeReader(PApplet p, Serial thisPort) {
    running = false;
    port = thisPort;
    parent = p;
    sleepRate = 1;

    try {
      xBeeMethod = parent.getClass().getMethod("xBeeEvent", new Class[] {XBeeReader.class});
    } catch (Exception e) {
      System.out.println("You forgot to implement the xBeeEvent() method.");
    }
  }
  /**
   * Initialises a new MidiIO object.
   *
   * @param parent
   */
  private MidiIO(processing.core.PApplet parent) {
    this.parent = parent;
    parent.registerDispose(this);

    try {
      noteOnMethod =
          parent
              .getClass()
              .getMethod("noteOn", new Class[] {Note.class, Integer.TYPE, Integer.TYPE});
    } catch (Exception e) {
      // no such method, or an error.. which is fine, just ignore
    }
    try {
      noteOffMethod =
          parent
              .getClass()
              .getMethod("noteOff", new Class[] {Note.class, Integer.TYPE, Integer.TYPE});
    } catch (Exception e) {
      // no such method, or an error.. which is fine, just ignore
    }
    try {
      controllerMethod =
          parent
              .getClass()
              .getMethod(
                  "controllerIn", new Class[] {Controller.class, Integer.TYPE, Integer.TYPE});
    } catch (Exception e) {
      // no such method, or an error.. which is fine, just ignore
    }
    try {
      programChangeMethod =
          parent
              .getClass()
              .getMethod(
                  "programChange", new Class[] {ProgramChange.class, Integer.TYPE, Integer.TYPE});
    } catch (Exception e) {
      // no such method, or an error.. which is fine, just ignore
    }
    getAvailablePorts();
  }
示例#6
0
  public XBeeReader(PApplet p, String thisFile, long sleepTime) {
    running = false;
    dataFile = thisFile;
    parent = p;
    fileArray = readFile(dataFile);
    fileIndex = 0;
    sleepRate = sleepTime;

    try {
      xBeeMethod = parent.getClass().getMethod("xBeeEvent", new Class[] {XBeeReader.class});
    } catch (Exception e) {
      System.out.println("You forgot to implement the xBeeEvent() method.");
    }
  }
  /**
   * Load model.
   *
   * @param pa the pa
   * @param pathToModel the path to model
   * @param creaseAngle the crease angle
   * @param flipTextureY flip texture y
   * @param flipTextureX flip texture x
   * @return the MT triangle meshes[]
   * @throws FileNotFoundException the file not found exception
   */
  public MTTriangleMesh[] loadModelImpl(
      PApplet pa, String pathToModel, float creaseAngle, boolean flipTextureY, boolean flipTextureX)
      throws FileNotFoundException {
    long timeA = System.currentTimeMillis();
    this.pa = pa;

    ArrayList<MTTriangleMesh> returnMeshList = new ArrayList<MTTriangleMesh>();

    TriangleNormalGenerator normalGenerator = new TriangleNormalGenerator();
    //		normalGenerator.setDebug(debug);

    HashMap<Integer, Group> materialIdToGroup = new HashMap<Integer, Group>();

    // TODO implement
    if (textureCache != null) textureCache.clear();
    textureCache = new WeakHashMap<String, PImage>();

    Scene3ds scene = null;

    try {
      TextDecode3ds decode = new TextDecode3ds();
      //			int level = Scene3ds.DECODE_USED_PARAMS_AND_CHUNKS; //Scene3ds.DECODE_ALL;
      // DECODE_USED_PARAMS, DECODE_USED_PARAMS_AND_CHUNKS
      int level = Scene3ds.DECODE_ALL;

      // LOAD all meshes from file into scene object
      File file = new File(pathToModel);
      if (file.exists()) {
        scene = new Scene3ds(file, decode, level);
      } else {
        InputStream in =
            Thread.currentThread().getContextClassLoader().getResourceAsStream(pathToModel);
        if (in == null) {
          in = pa.getClass().getResourceAsStream(pathToModel);
        }
        if (in != null) {
          scene = new Scene3ds(in, decode, level);
        } else {
          throw new FileNotFoundException("File not found: " + file.getAbsolutePath());
        }
      }

      if (debug) logger.debug("\n-> Loading model: " + file.getName() + " <-");

      if (debug) {
        // Go through all MATERIALS
        logger.debug("\nNum Scene Materials: " + scene.materials());
        for (int m = 0; m < scene.materials(); m++) {
          Material3ds mat = scene.material(m);
          logger.debug("  Material " + m + ": \" " + mat.name() + "\"");
        }
        logger.debug("");
      }

      ///////// Go through all MESHES //////////////////////////
      for (int i = 0; i < scene.meshes(); i++) {
        Mesh3ds m = scene.mesh(i);

        if (debug) {
          int texMapType = m.texMapType();
          logger.debug("Texture coordinates provided: " + m.texCoords());
          logger.debug("Texture mapping type: " + texMapType);
          logger.debug("Mesh:" + m.name() + " Pivot:" + m.pivot());
        }

        /*
        XYZTrack3ds pos 		= m.positionTrack();
        RotationTrack3ds rot 	= m.rotationTrack();
        XYZTrack3ds sc 			= m.scaleTrack();
        //FIXME .track and key(i) werden nicht zur verfügung gestellt?!? aber in javadoc
        */

        if (debug) {
          logger.debug(
              "->Processing mesh: " + i + " of " + scene.meshes() + " Name: \"" + m.name() + "\"");
          logger.debug("  Num Faces: " + m.faces());
          logger.debug("  Num Vertices: " + m.vertices());
          logger.debug("  Num TextureCoordinates: " + m.texCoords());
          logger.debug("");
        }

        // Create the arrays needed for the cruncher
        Vertex[] vertices = new Vertex[m.vertices()];
        int[] indices = new int[m.faces() * 3];

        int[] texCoordIndices = new int[m.faces() * 3];
        float[][] textureCoords = new float[m.texCoords()][2];

        // Fill Vertices array
        for (int j = 0; j < m.vertices(); j++) {
          Vertex3ds vert = m.vertex(j);

          if (this.flipY) {
            vertices[j] = new Vertex(vert.X, -vert.Y, vert.Z, -1, -1);
          } else {
            vertices[j] = new Vertex(vert.X, vert.Y, vert.Z, -1, -1);
          }

          if (m.texCoords() > j) textureCoords[j] = new float[] {m.texCoord(j).U, m.texCoord(j).V};
        }

        // Fill texcoords array
        for (int j = 0; j < m.texCoords(); j++) {
          TexCoord3ds tex = m.texCoord(j);

          float[] texCoord = new float[2];
          texCoord[0] = tex.U;
          texCoord[1] = tex.V;

          textureCoords[j] = texCoord;
        }

        // TODO so werden gleiche materials in verschiedenen meshes nicht zu einem mesh gemacht!
        // also müsste also ohne clear machen und dafür vertices + texcoords in einen grossen
        // array, dafür müssten indices aber per offset angepasst werden dass die wieder stimmen!
        materialIdToGroup.clear();

        if (m.faceMats() > 0) {
          // List face Materials  //TODO USE LOGGERS!!
          logger.debug("  Num Face-Materials: " + m.faceMats());
          for (int n = 0; n < m.faceMats(); n++) {
            FaceMat3ds fmat = m.faceMat(n);
            logger.debug("    FaceMat ID: " + fmat.matIndex());
            logger.debug("    FaceMat indices: " + fmat.faceIndexes());

            int[] faceIndicesForMaterial = fmat.faceIndex();
            if (faceIndicesForMaterial.length <= 0) {
              continue;
            }

            // Check if there already is a group with the same material
            Group group = materialIdToGroup.get(fmat.matIndex());

            // If not, create a new group and save it in map
            if (group == null) {
              group = new Group(new Integer(fmat.matIndex()).toString());
              materialIdToGroup.put(fmat.matIndex(), group);
            }

            // Go through all pointers to the faces for this material
            // and get the corresponding face
            for (int j = 0; j < faceIndicesForMaterial.length; j++) {
              int k = faceIndicesForMaterial[j];
              Face3ds face = m.face(k);

              AFace aFace = new AFace();
              aFace.p0 = face.P0;
              aFace.p1 = face.P1;
              aFace.p2 = face.P2;

              aFace.t0 = face.P0;
              aFace.t1 = face.P1;
              aFace.t2 = face.P2;

              group.addFace(aFace);
            }
          }

          Iterator<Integer> it = materialIdToGroup.keySet().iterator();
          logger.debug("Mesh: " + m.name() + " Anzahl Groups:" + materialIdToGroup.keySet().size());
          while (it.hasNext()) {
            int currentGroupName = it.next();
            Group currentGroup = materialIdToGroup.get(currentGroupName);
            logger.debug("Current group: " + currentGroupName);

            currentGroup.compileItsOwnLists(vertices, textureCoords);

            // Get the new arrays
            Vertex[] newVertices = currentGroup.getGroupVertices();
            int[] newIndices = currentGroup.getIndexArray();
            float[][] newTextureCoords = currentGroup.getGroupTexCoords();
            int[] newTexIndices = currentGroup.getTexCoordIndices();

            logger.debug(
                "\nGroup: \""
                    + currentGroup.name
                    + "\" ->Vertices: "
                    + currentGroup.verticesForGroup.size()
                    + " ->TextureCoords: "
                    + currentGroup.texCoordsForGroup.size()
                    + " ->Indices: "
                    + currentGroup.indexArray.length
                    + " ->Texcoord Indices: "
                    + currentGroup.texCoordIndexArray.length);
            logger.debug("");

            if (vertices.length > 2) {
              GeometryInfo geometry = null;
              // Load as all vertex normals smoothed if creaseAngle == 180;
              if (creaseAngle == 180) {
                geometry =
                    normalGenerator.generateSmoothNormals(
                        pa,
                        newVertices,
                        newIndices,
                        newTextureCoords,
                        newTexIndices,
                        creaseAngle,
                        flipTextureY,
                        flipTextureX);
              } else {
                geometry =
                    normalGenerator.generateCreaseAngleNormals(
                        pa,
                        newVertices,
                        newIndices,
                        newTextureCoords,
                        newTexIndices,
                        creaseAngle,
                        flipTextureY,
                        flipTextureX);
              }

              MTTriangleMesh mesh = new MTTriangleMesh(pa, geometry);

              if (mesh != null) {
                mesh.setName(m.name() + " material: " + new Integer(currentGroupName).toString());
                // Assign texture
                this.assignMaterial(pathToModel, file, scene, m, currentGroupName, mesh);

                if (mesh.getTexture() != null) {
                  mesh.setTextureEnabled(true);
                } else {
                  logger.debug("No texture could be assigned to mesh.");
                }
                returnMeshList.add(mesh);
              }
            }
          }
        } else {
          // If there are no materials for this mesh dont split mesh into
          // groups by material
          // Fill indices array and texcoords array (Here: vertex index = texcoord index)
          for (int faceIndex = 0; faceIndex < m.faces(); faceIndex++) {
            Face3ds f = m.face(faceIndex);

            indices[faceIndex * 3] = f.P0;
            indices[faceIndex * 3 + 1] = f.P1;
            indices[faceIndex * 3 + 2] = f.P2;

            texCoordIndices[faceIndex * 3] = f.P0;
            texCoordIndices[faceIndex * 3 + 1] = f.P1;
            texCoordIndices[faceIndex * 3 + 2] = f.P2;
          } // for faces

          // Create the Mesh and set a texture
          if (vertices.length > 2) {
            // Create normals for the mesh and duplicate vertices for faces that share the same
            // Vertex, but with different texture coordinates or different normals
            GeometryInfo geometry = null;
            // Generate normals and denormalize vertices with more than 1 texture coordinate
            if (creaseAngle == 180) {
              geometry =
                  normalGenerator.generateSmoothNormals(
                      pa,
                      vertices,
                      indices,
                      textureCoords,
                      texCoordIndices,
                      creaseAngle,
                      flipTextureY,
                      flipTextureX);
            } else {
              geometry =
                  normalGenerator.generateCreaseAngleNormals(
                      pa,
                      vertices,
                      indices,
                      textureCoords,
                      texCoordIndices,
                      creaseAngle,
                      flipTextureY,
                      flipTextureX);
            }
            MTTriangleMesh mesh = new MTTriangleMesh(pa, geometry);
            mesh.setName(m.name());
            //						this.assignMaterial(file, scene, m, sceneMaterialID, mesh);
            returnMeshList.add(mesh);
          } // end if vertices.lentgh > 2
        }
      } // for meshes

      //			logger.debug(decode.text());
    } catch (Exception e) {
      e.printStackTrace();
    }

    materialIdToGroup.clear();

    long timeB = System.currentTimeMillis();
    long delta = timeB - timeA;
    logger.debug("Loaded model in: " + delta + " ms");
    return (MTTriangleMesh[]) returnMeshList.toArray(new MTTriangleMesh[returnMeshList.size()]);
  }