Commits

bergsoe committed 1c6dfad

Rename the C wrapper to c_papl_yaobi.

'papl' is added as a prefix to the C library files.

  • Participants
  • Parent commits c9b8bb5

Comments (0)

Files changed (9)

   CompiledObject: native
   Path:	    src
   Modules:  PaplYaobi
-  CSources: yaobi_stub.c
-  CCLib:    -lc_yaobi -lyaobi -lstdc++
+  CSources: stub.c
+  CCLib:    -lc_papl_yaobi -lyaobi -lstdc++
   Install:  true
   BuildDepends: papl, batteries, threads

File c_yaobi/CMakeLists.txt

 set(CMAKE_BUILD_TYPE Release)
 add_definitions(-DNDEBUG)
 
-add_library(c_yaobi c_yaobi.cpp)
+add_library(c_papl_yaobi papl_yaobi.cpp)

File c_yaobi/c_yaobi.cpp

-/*
-  Copyright (c) 2012 Anders Lau Olsen.
-  See LICENSE file for terms and conditions.
-*/
-
-#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();
-}

File c_yaobi/c_yaobi.h

-/*
-  Copyright (c) 2012 Anders Lau Olsen.
-  See LICENSE file for terms and conditions.
-*/
-
-/*
-  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

File c_yaobi/install.sh

 #!/bin/bash
 
-sudo cp *.h /usr/local/include/yaobi
-sudo cp libc_yaobi.a /usr/local/lib
+sudo cp *.h /usr/local/include
+sudo cp libc_papl_yaobi.a /usr/local/lib

File c_yaobi/papl_yaobi.cpp

+/*
+  Copyright (c) 2012 Anders Lau Olsen.
+  See LICENSE file for terms and conditions.
+*/
+
+#include "papl_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();
+}

File c_yaobi/papl_yaobi.h

+/*
+  Copyright (c) 2012 Anders Lau Olsen.
+  See LICENSE file for terms and conditions.
+*/
+
+/*
+  The c_papl_yaobi library is a C interface to a subset of the Yaobi collision
+  detection library.
+*/
+
+#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
+/*
+  Copyright (c) 2012 Anders Lau Olsen.
+  See LICENSE file for terms and conditions.
+*/
+
+#include <caml/mlvalues.h>
+#include <caml/memory.h>
+#include <caml/alloc.h>
+#include <caml/custom.h>
+
+#include <papl_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);
+}
+
+static 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)));
+}

File src/yaobi_stub.c

-/*
-  Copyright (c) 2012 Anders Lau Olsen.
-  See LICENSE file for terms and conditions.
-*/
-
-#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)));
-}