Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added UV Texture Predictor #34

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Sim3DR/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.DS_Store
cmake-build-debug/
.idea/
build/
*.so
data/

lib/rasterize.cpp
29 changes: 29 additions & 0 deletions Sim3DR/Sim3DR.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# coding: utf-8

from . import _init_paths
import numpy as np
import Sim3DR_Cython


def get_normal(vertices, triangles):
normal = np.zeros_like(vertices, dtype=np.float32)
Sim3DR_Cython.get_normal(normal, vertices, triangles, vertices.shape[0], triangles.shape[0])
return normal


def rasterize(vertices, triangles, colors, bg=None,
height=None, width=None, channel=None,
reverse=False):
if bg is not None:
height, width, channel = bg.shape
else:
assert height is not None and width is not None and channel is not None
bg = np.zeros((height, width, channel), dtype=np.uint8)

buffer = np.zeros((height, width), dtype=np.float32) - 1e8

if colors.dtype != np.float32:
colors = colors.astype(np.float32)
Sim3DR_Cython.rasterize(bg, vertices, triangles, colors, buffer, triangles.shape[0], height, width, channel,
reverse=reverse)
return bg
4 changes: 4 additions & 0 deletions Sim3DR/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# coding: utf-8

from .Sim3DR import get_normal, rasterize
from .lighting import RenderPipeline
14 changes: 14 additions & 0 deletions Sim3DR/_init_paths.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# coding: utf-8

import os.path as osp
import sys


def add_path(path):
if path not in sys.path:
sys.path.insert(0, path)


this_dir = osp.dirname(__file__)
lib_path = osp.join(this_dir, '.')
add_path(lib_path)
1 change: 1 addition & 0 deletions Sim3DR/build_sim3dr.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python3 setup.py build_ext --inplace
115 changes: 115 additions & 0 deletions Sim3DR/lib/rasterize.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#ifndef MESH_CORE_HPP_
#define MESH_CORE_HPP_

#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <string>
#include <iostream>
#include <fstream>

using namespace std;

class Point3D {
public:
float x;
float y;
float z;

public:
Point3D() : x(0.f), y(0.f), z(0.f) {}
Point3D(float x_, float y_, float z_) : x(x_), y(y_), z(z_) {}

void initialize(float x_, float y_, float z_){
this->x = x_; this->y = y_; this->z = z_;
}

Point3D cross(Point3D &p){
Point3D c;
c.x = this->y * p.z - this->z * p.y;
c.y = this->z * p.x - this->x * p.z;
c.z = this->x * p.y - this->y * p.x;
return c;
}

float dot(Point3D &p) {
return this->x * p.x + this->y * p.y + this->z * p.z;
}

Point3D operator-(const Point3D &p) {
Point3D np;
np.x = this->x - p.x;
np.y = this->y - p.y;
np.z = this->z - p.z;
return np;
}

};

class Point {
public:
float x;
float y;

public:
Point() : x(0.f), y(0.f) {}
Point(float x_, float y_) : x(x_), y(y_) {}
float dot(Point p) {
return this->x * p.x + this->y * p.y;
}

Point operator-(const Point &p) {
Point np;
np.x = this->x - p.x;
np.y = this->y - p.y;
return np;
}

Point operator+(const Point &p) {
Point np;
np.x = this->x + p.x;
np.y = this->y + p.y;
return np;
}

Point operator*(float s) {
Point np;
np.x = s * this->x;
np.y = s * this->y;
return np;
}
};


bool is_point_in_tri(Point p, Point p0, Point p1, Point p2);

void get_point_weight(float *weight, Point p, Point p0, Point p1, Point p2);

void _get_tri_normal(float *tri_normal, float *vertices, int *triangles, int ntri, bool norm_flg);

void _get_ver_normal(float *ver_normal, float *tri_normal, int *triangles, int nver, int ntri);

void _get_normal(float *ver_normal, float *vertices, int *triangles, int nver, int ntri);

void _rasterize_triangles(
float *vertices, int *triangles, float *depth_buffer, int *triangle_buffer, float *barycentric_weight,
int ntri, int h, int w);

void _rasterize(
unsigned char *image, float *vertices, int *triangles, float *colors,
float *depth_buffer, int ntri, int h, int w, int c, float alpha, bool reverse);

void _render_texture_core(
float *image, float *vertices, int *triangles,
float *texture, float *tex_coords, int *tex_triangles,
float *depth_buffer,
int nver, int tex_nver, int ntri,
int h, int w, int c,
int tex_h, int tex_w, int tex_c,
int mapping_type);

void _write_obj_with_colors_texture(string filename, string mtl_name,
float *vertices, int *triangles, float *colors, float *uv_coords,
int nver, int ntri, int ntexver);

#endif
134 changes: 134 additions & 0 deletions Sim3DR/lib/rasterize.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import numpy as np
cimport numpy as np
# from libcpp.string cimport string
cimport cython
from libcpp cimport bool

# from cpython import bool

# use the Numpy-C-API from Cython
np.import_array()

# cdefine the signature of our c function
cdef extern from "rasterize.h":
void _rasterize_triangles(
float*vertices, int*triangles, float*depth_buffer, int*triangle_buffer, float*barycentric_weight,
int ntri, int h, int w
)

void _rasterize(
unsigned char*image, float*vertices, int*triangles, float*colors, float*depth_buffer,
int ntri, int h, int w, int c, float alpha, bool reverse
)

# void _render_texture_core(
# float* image, float* vertices, int* triangles,
# float* texture, float* tex_coords, int* tex_triangles,
# float* depth_buffer,
# int nver, int tex_nver, int ntri,
# int h, int w, int c,
# int tex_h, int tex_w, int tex_c,
# int mapping_type)

void _get_tri_normal(float *tri_normal, float *vertices, int *triangles, int nver, bool norm_flg)
void _get_ver_normal(float *ver_normal, float*tri_normal, int*triangles, int nver, int ntri)
void _get_normal(float *ver_normal, float *vertices, int *triangles, int nver, int ntri)


# void _write_obj_with_colors_texture(string filename, string mtl_name,
# float* vertices, int* triangles, float* colors, float* uv_coords,
# int nver, int ntri, int ntexver)

@cython.boundscheck(False)
@cython.wraparound(False)
def get_tri_normal(np.ndarray[float, ndim=2, mode="c"] tri_normal not None,
np.ndarray[float, ndim=2, mode = "c"] vertices not None,
np.ndarray[int, ndim=2, mode="c"] triangles not None,
int ntri, bool norm_flg = False):
_get_tri_normal(<float*> np.PyArray_DATA(tri_normal), <float*> np.PyArray_DATA(vertices),
<int*> np.PyArray_DATA(triangles), ntri, norm_flg)

@cython.boundscheck(False) # turn off bounds-checking for entire function
@cython.wraparound(False) # turn off negative index wrapping for entire function
def get_ver_normal(np.ndarray[float, ndim=2, mode = "c"] ver_normal not None,
np.ndarray[float, ndim=2, mode = "c"] tri_normal not None,
np.ndarray[int, ndim=2, mode="c"] triangles not None,
int nver, int ntri):
_get_ver_normal(
<float*> np.PyArray_DATA(ver_normal), <float*> np.PyArray_DATA(tri_normal), <int*> np.PyArray_DATA(triangles),
nver, ntri)

@cython.boundscheck(False) # turn off bounds-checking for entire function
@cython.wraparound(False) # turn off negative index wrapping for entire function
def get_normal(np.ndarray[float, ndim=2, mode = "c"] ver_normal not None,
np.ndarray[float, ndim=2, mode = "c"] vertices not None,
np.ndarray[int, ndim=2, mode="c"] triangles not None,
int nver, int ntri):
_get_normal(
<float*> np.PyArray_DATA(ver_normal), <float*> np.PyArray_DATA(vertices), <int*> np.PyArray_DATA(triangles),
nver, ntri)


@cython.boundscheck(False) # turn off bounds-checking for entire function
@cython.wraparound(False) # turn off negative index wrapping for entire function
def rasterize_triangles(
np.ndarray[float, ndim=2, mode = "c"] vertices not None,
np.ndarray[int, ndim=2, mode="c"] triangles not None,
np.ndarray[float, ndim=2, mode = "c"] depth_buffer not None,
np.ndarray[int, ndim=2, mode = "c"] triangle_buffer not None,
np.ndarray[float, ndim=2, mode = "c"] barycentric_weight not None,
int ntri, int h, int w
):
_rasterize_triangles(
<float*> np.PyArray_DATA(vertices), <int*> np.PyArray_DATA(triangles),
<float*> np.PyArray_DATA(depth_buffer), <int*> np.PyArray_DATA(triangle_buffer),
<float*> np.PyArray_DATA(barycentric_weight),
ntri, h, w)

@cython.boundscheck(False) # turn off bounds-checking for entire function
@cython.wraparound(False) # turn off negative index wrapping for entire function
def rasterize(np.ndarray[unsigned char, ndim=3, mode = "c"] image not None,
np.ndarray[float, ndim=2, mode = "c"] vertices not None,
np.ndarray[int, ndim=2, mode="c"] triangles not None,
np.ndarray[float, ndim=2, mode = "c"] colors not None,
np.ndarray[float, ndim=2, mode = "c"] depth_buffer not None,
int ntri, int h, int w, int c, float alpha = 1, bool reverse = False
):
_rasterize(
<unsigned char*> np.PyArray_DATA(image), <float*> np.PyArray_DATA(vertices),
<int*> np.PyArray_DATA(triangles),
<float*> np.PyArray_DATA(colors),
<float*> np.PyArray_DATA(depth_buffer),
ntri, h, w, c, alpha, reverse)

# def render_texture_core(np.ndarray[float, ndim=3, mode = "c"] image not None,
# np.ndarray[float, ndim=2, mode = "c"] vertices not None,
# np.ndarray[int, ndim=2, mode="c"] triangles not None,
# np.ndarray[float, ndim=3, mode = "c"] texture not None,
# np.ndarray[float, ndim=2, mode = "c"] tex_coords not None,
# np.ndarray[int, ndim=2, mode="c"] tex_triangles not None,
# np.ndarray[float, ndim=2, mode = "c"] depth_buffer not None,
# int nver, int tex_nver, int ntri,
# int h, int w, int c,
# int tex_h, int tex_w, int tex_c,
# int mapping_type
# ):
# _render_texture_core(
# <float*> np.PyArray_DATA(image), <float*> np.PyArray_DATA(vertices), <int*> np.PyArray_DATA(triangles),
# <float*> np.PyArray_DATA(texture), <float*> np.PyArray_DATA(tex_coords), <int*> np.PyArray_DATA(tex_triangles),
# <float*> np.PyArray_DATA(depth_buffer),
# nver, tex_nver, ntri,
# h, w, c,
# tex_h, tex_w, tex_c,
# mapping_type)
#
# def write_obj_with_colors_texture_core(string filename, string mtl_name,
# np.ndarray[float, ndim=2, mode = "c"] vertices not None,
# np.ndarray[int, ndim=2, mode="c"] triangles not None,
# np.ndarray[float, ndim=2, mode = "c"] colors not None,
# np.ndarray[float, ndim=2, mode = "c"] uv_coords not None,
# int nver, int ntri, int ntexver
# ):
# _write_obj_with_colors_texture(filename, mtl_name,
# <float*> np.PyArray_DATA(vertices), <int*> np.PyArray_DATA(triangles), <float*> np.PyArray_DATA(colors), <float*> np.PyArray_DATA(uv_coords),
# nver, ntri, ntexver)
Loading