bergsoe avatar bergsoe committed fb515a6

Initial commit.

Comments (0)

Files changed (8)

+OASISFormat: 0.2
+OCamlVersion: >= 3.12
+Name: Papl_yaobi
+Version: 0.0.0
+Synopsis: Yaobi wrapper for Papl
+Authors: Anders Lau Olsen
+Copyrights: (C) 2011 Anders Lau Olsen
+License: LGPL-2.1 with OCaml linking exception
+LicenseFile: LICENSE
+BuildTools: ocamlbuild
+Plugins:
+  META (0.2),
+  StdFiles (0.2)
+
+XStdFilesREADME: false
+XStdFilesREADMEFilename: README.txt
+XStdFilesINSTALL: true
+XStdFilesINSTALLFilename: INSTALL.txt
+XStdFilesAUTHORS: false
+XStdFilesAUTHORSFilename: AUTHORS.txt
+
+#----------------------------------------------------------------------
+
+Library papl_yaobi
+  Build$: flag(is_native)
+  CompiledObject: native
+  Path:	    src
+  Modules:  PaplYaobi
+  CSources: yaobi_stub.c
+  CCLib:    -lc_yaobi -lyaobi -lstdc++
+  Install:  true
+  BuildDepends: papl, batteries, threads

c_yaobi/CMakeLists.txt

+cmake_minimum_required(VERSION 2.8)
+
+set(CMAKE_BUILD_TYPE Release)
+add_definitions(-DNDEBUG)
+
+add_library(c_yaobi c_yaobi.cpp)

c_yaobi/c_yaobi.cpp

+#include "c_yaobi.h"
+
+#include <yaobi/yaobi.h>
+#include <yaobi/yaobi_mesh_interface.h>
+#include <malloc.h>
+#include <iostream>
+
+using namespace yaobi;
+
+struct Yaobi_CollModel
+{
+    CollModel model;
+    double* vertices;
+    int* triangles;
+    int own_data;
+
+    Yaobi_CollModel(
+        TriMeshInterface* mesh,
+        double* vertices,
+        int* triangles,
+        int own_data) :
+        model(mesh, OWN_DATA),
+        vertices(vertices),
+        triangles(triangles),
+        own_data(own_data)
+    {}
+
+    ~Yaobi_CollModel()
+    {
+        if (own_data) {
+            // Since we pretend to be a C library the arrays must have been
+            // allocated with malloc(), so we delete them with free().
+            free(vertices);
+            free(triangles);
+        }
+    }
+
+private:
+    Yaobi_CollModel(const Yaobi_CollModel&);
+    Yaobi_CollModel& operator=(const Yaobi_CollModel&);
+};
+
+Yaobi_CollModel* yaobi_buildCollModel(
+    int num_vertices, double* vertices,
+    int num_triangles, int* triangles,
+    int triangle_stride,
+    int own_data)
+{
+    TriMeshInterface* mesh =
+        new TriMeshInterface(
+            num_vertices, vertices,
+            num_triangles, triangles,
+            triangle_stride, SHARE_DATA);
+    Yaobi_CollModel* model = new Yaobi_CollModel(
+        mesh, vertices, triangles, own_data);
+
+    build_obb_tree(model->model, OWN_DATA);
+    return model;
+}
+
+void yaobi_freeCollModel(Yaobi_CollModel* model)
+{
+    delete model;
+}
+
+int yaobi_isColliding(
+    double ta_[][4], Yaobi_CollModel* a,
+    double tb_[][4], Yaobi_CollModel* b)
+{
+    float ta[3][4];
+    float tb[3][4];
+    for (int i = 0; i < 3; i++)
+        for (int j = 0; j < 3; j++)
+            ta[i][j] = static_cast<float>(ta_[i][j]);
+    for (int i = 0; i < 3; i++)
+        for (int j = 0; j < 3; j++)
+            tb[i][j] = static_cast<float>(tb_[i][j]);
+
+    CollideResult res;
+    Collide(res, ta_, a->model, tb_, b->model, FIRST_CONTACT_ONLY);
+    return res.IsColliding();
+}

c_yaobi/c_yaobi.h

+/*
+  c_yaobi.h is a C interface to the Yaobi library.
+
+  We are compiling this interface into the standard Yaobi library, so that there
+  is only one library to link against.
+
+  We are only writing C functions for a subset of Yaobi's interface.
+*/
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct Yaobi_CollModel Yaobi_CollModel;
+
+extern Yaobi_CollModel* yaobi_buildCollModel(
+    int num_vertices, double* vertices,
+    int num_triangles, int* triangles,
+    int triangle_stride,
+    int own_data);
+
+extern void yaobi_freeCollModel(Yaobi_CollModel*);
+
+extern int yaobi_isColliding(
+    double ta[][4], Yaobi_CollModel* a,
+    double tb[][4], Yaobi_CollModel* b);
+
+#ifdef __cplusplus
+}
+#endif 

c_yaobi/install.sh

+#!/bin/bash
+
+sudo cp *.h /usr/local/include/yaobi
+sudo cp libc_yaobi.a /usr/local/lib
+
+type t
+
+external caml_yaobi_build :
+  float array -> int -> t = "caml_yaobi_build"
+
+external caml_yaobi_in_collision :
+  float array -> t ->
+  float array -> t ->
+  bool = "caml_yaobi_in_collision"
+
+type geom_t = PaplVector.V3D.triangle_geom_t
+
+type transform_t = float array
+
+let build triangles =
+  let buf = Array.make (9 * Array.length triangles) 0. in
+  let write_vertex p (x, y, z) =
+    buf.(p + 0) <- x;
+    buf.(p + 1) <- y;
+    buf.(p + 2) <- z
+  in
+  let write_triangle p (v0, v1, v2) =
+    write_vertex (p + 0) v0;
+    write_vertex (p + 3) v1;
+    write_vertex (p + 6) v2
+  in
+  let () =
+    Array.iteri
+      (fun i t -> write_triangle (9 * i) t)
+      triangles
+  in
+    caml_yaobi_build buf (Array.length buf)
+
+let to_transform = PaplTransform.SE3.to_array
+
+let in_collision ta a tb b =
+  caml_yaobi_in_collision ta a tb b
+
+module V2D = struct
+  module SE = PaplTransform.SE2
+  module SO = PaplTransform.SO2
+
+  let to_transform t =
+    let ((x, y), rot) = SE.get t in
+    let a = SO.angle rot in
+    let ca = cos a in
+    let sa = sin a in
+      [| ca; -.sa; 0.; x;
+         sa; ca; 0.; y;
+         0.; 0.; 1.; 0. |]
+end

src/PaplYaobi.mli

+
+type t
+
+type geom_t = PaplVector.V3D.triangle_geom_t
+
+type transform_t
+
+val build : geom_t -> t
+
+val to_transform : PaplTransform.SE3.t -> transform_t
+
+val in_collision :
+  transform_t -> t ->
+  transform_t -> t ->
+  bool
+
+module V2D : sig
+  val to_transform : PaplTransform.SE2.t -> transform_t
+end
+
+#include <caml/mlvalues.h>
+#include <caml/memory.h>
+#include <caml/alloc.h>
+#include <caml/custom.h>
+
+#include <yaobi/c_yaobi.h>
+#include <stdio.h>
+
+typedef Yaobi_CollModel CollModel;
+
+#define CollModel_val(v) (*((CollModel **) Data_custom_val(v)))
+
+static void finalize(value model)
+{
+    /* This does work, but beware that GC isn't done before the program
+       exits. */
+    yaobi_freeCollModel(CollModel_val(model));
+}
+
+static struct custom_operations coll_model_ops = {
+  "org.bergsoe.caml_yaobi",
+  finalize,
+  custom_compare_default,
+  custom_hash_default,
+  custom_serialize_default,
+  custom_deserialize_default
+};
+
+value caml_yaobi_build(value triangles_value, value len_value)
+{
+    CAMLparam2(triangles_value, len_value);
+
+    int num_vertices = Int_val(len_value);
+    double* vertices = malloc(num_vertices * sizeof(double));
+    int num_triangles = num_vertices / 9;
+    int* triangles = malloc(num_vertices * sizeof(int));
+    int own_data = 1;
+
+    int i;
+    for (i = 0; i < num_vertices; i++) {
+        vertices[i] = Double_field(triangles_value, i);
+        triangles[i] = i;
+    }
+    
+    value model = alloc_custom(&coll_model_ops, sizeof(CollModel *), 0, 1);
+    CollModel_val(model) =
+        yaobi_buildCollModel(
+            num_vertices, vertices,
+            num_triangles, triangles,
+            3,
+            own_data);
+    CAMLreturn(model);
+}
+
+void read_transform(
+    value t_value,
+    double t[3][4])
+{
+    int i;
+    int j;
+    int p = 0;
+
+    for (i = 0; i < 3; i++) {
+        for (j = 0; j < 4; j++)
+            t[i][j] = Double_field(t_value, p++);
+    }
+}
+
+value caml_yaobi_in_collision(
+    value ta_value, value ma_value,
+    value tb_value, value mb_value)
+{
+    CAMLparam4(ta_value, ma_value, tb_value, mb_value);
+
+    double ta[3][4];
+    double tb[3][4];
+    read_transform(ta_value, ta);
+    read_transform(tb_value, tb);
+
+    Yaobi_CollModel* ma = CollModel_val(ma_value);
+    Yaobi_CollModel* mb = CollModel_val(mb_value);
+
+    CAMLreturn(Val_bool(yaobi_isColliding(ta, ma, tb, mb)));
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.