Correct geometry creation code with Curves.

Also make `Geometry` and `GlobalVertices` members of
`CurveFromGeometry` as `const &` to match with
`MeshFromGeometry`.
This commit is contained in:
2020-08-10 23:58:21 +05:30
parent a0f21e47ae
commit d062c712e2
6 changed files with 39 additions and 21 deletions

View File

@@ -184,29 +184,35 @@ BLI_INLINE void copy_string_to_int(Span<string_view> src,
* this function returns true.
*/
static Geometry *create_geometry(Geometry *const prev_geometry,
const eGeometryType type,
const eGeometryType new_type,
string_view name,
Vector<std::unique_ptr<Geometry>> &r_all_geometries)
{
auto new_geometry = [&]() {
if (name.empty()) {
r_all_geometries.append(std::make_unique<Geometry>(type, "New object"));
r_all_geometries.append(std::make_unique<Geometry>(new_type, "New object"));
}
else {
r_all_geometries.append(std::make_unique<Geometry>(type, name));
r_all_geometries.append(std::make_unique<Geometry>(new_type, name));
}
return r_all_geometries.last().get();
};
if (prev_geometry && prev_geometry->geom_type() & GEOM_MESH) {
if (prev_geometry && prev_geometry->get_geom_type() == GEOM_MESH) {
/* After the creation of a Geometry instance, at least one element has been found in the OBJ
* file that indicates that it is a mesh. */
if (prev_geometry->tot_face_elems() || prev_geometry->tot_normals()) {
return new_geometry();
}
if (new_type == GEOM_CURVE) {
/* The object originally created is not a mesh now that curve data
* follows the vertex coordinates list. */
prev_geometry->set_geom_type(GEOM_CURVE);
return prev_geometry;
}
}
if (prev_geometry && prev_geometry->geom_type() & GEOM_CURVE) {
if (prev_geometry && prev_geometry->get_geom_type() == GEOM_CURVE) {
return new_geometry();
}

View File

@@ -30,11 +30,10 @@ namespace blender::io::obj {
/**
* Create a NURBS spline for the Curve converted from Geometry.
*/
void CurveFromGeometry::create_nurbs(const Geometry &curve_geometry,
const GlobalVertices &global_vertices)
void CurveFromGeometry::create_nurbs()
{
const int64_t tot_vert{curve_geometry.nurbs_elem().curv_indices.size()};
const NurbsElement &nurbs_geometry = curve_geometry.nurbs_elem();
const int64_t tot_vert{curve_geometry_.nurbs_elem().curv_indices.size()};
const NurbsElement &nurbs_geometry = curve_geometry_.nurbs_elem();
Nurb *nurb = static_cast<Nurb *>(blender_curve_->nurb.first);
nurb->type = CU_NURBS;
@@ -51,7 +50,7 @@ void CurveFromGeometry::create_nurbs(const Geometry &curve_geometry,
BKE_nurb_points_add(nurb, tot_vert);
for (int i = 0; i < tot_vert; i++) {
BPoint &bpoint = nurb->bp[i];
copy_v3_v3(bpoint.vec, global_vertices.vertices[nurbs_geometry.curv_indices[i]]);
copy_v3_v3(bpoint.vec, global_vertices_.vertices[nurbs_geometry.curv_indices[i]]);
bpoint.vec[3] = 1.0f;
bpoint.weight = 1.0f;
}
@@ -86,15 +85,16 @@ void CurveFromGeometry::create_nurbs(const Geometry &curve_geometry,
CurveFromGeometry::CurveFromGeometry(Main *bmain,
const Geometry &geometry,
const GlobalVertices &global_vertices)
: curve_geometry_(geometry), global_vertices_(global_vertices)
{
std::string ob_name = geometry.geometry_name();
if (ob_name.empty() && !geometry.group().empty()) {
ob_name = geometry.group();
std::string ob_name = curve_geometry_.geometry_name();
if (ob_name.empty() && !curve_geometry_.group().empty()) {
ob_name = curve_geometry_.group();
}
else {
ob_name = "Untitled";
}
blender_curve_.reset(BKE_curve_add(bmain, geometry.geometry_name().c_str(), OB_CURVE));
blender_curve_.reset(BKE_curve_add(bmain, curve_geometry_.geometry_name().c_str(), OB_CURVE));
curve_object_.reset(BKE_object_add_only_object(bmain, OB_CURVE, ob_name.c_str()));
blender_curve_->flag = CU_3D;
@@ -104,7 +104,7 @@ CurveFromGeometry::CurveFromGeometry(Main *bmain,
Nurb *nurb = static_cast<Nurb *>(MEM_callocN(sizeof(Nurb), "OBJ import NURBS curve"));
BLI_addtail(BKE_curve_nurbs_get(blender_curve_.get()), nurb);
create_nurbs(geometry, global_vertices);
create_nurbs();
curve_object_->data = blender_curve_.release();
}

View File

@@ -58,6 +58,8 @@ class CurveFromGeometry : NonMovable, NonCopyable {
* Object of type OB_CURVE. Use the mover function to own it.
*/
unique_object_ptr curve_object_;
const Geometry &curve_geometry_;
const GlobalVertices &global_vertices_;
public:
CurveFromGeometry(Main *bmain,
@@ -70,6 +72,6 @@ class CurveFromGeometry : NonMovable, NonCopyable {
}
private:
void create_nurbs(const Geometry &curve_geometry, const GlobalVertices &global_vertices);
void create_nurbs();
};
} // namespace blender::io::obj

View File

@@ -36,11 +36,20 @@
namespace blender::io::obj {
eGeometryType Geometry::geom_type() const
eGeometryType Geometry::get_geom_type() const
{
return geom_type_;
}
/**
* Use very rarely. Only when it is guaranteed that the
* type originally set is wrong.
*/
void Geometry::set_geom_type(const eGeometryType new_type)
{
geom_type_ = new_type;
}
const std::string &Geometry::geometry_name() const
{
return geometry_name_;

View File

@@ -113,7 +113,7 @@ enum eGeometryType {
class Geometry {
private:
const eGeometryType geom_type_ = GEOM_MESH;
eGeometryType geom_type_ = GEOM_MESH;
std::string geometry_name_{};
Vector<std::string> material_name_{};
/**
@@ -133,7 +133,8 @@ class Geometry {
Geometry(eGeometryType type, std::string_view ob_name)
: geom_type_(type), geometry_name_(std::string(ob_name)){};
eGeometryType geom_type() const;
eGeometryType get_geom_type() const;
void set_geom_type(const eGeometryType new_type);
const std::string &geometry_name() const;
int vertex_indices_lookup(const int key) const;
int64_t tot_verts() const;

View File

@@ -94,11 +94,11 @@ static void geometry_to_blender_objects(Main *bmain,
{
OBJImportCollection import_collection{bmain, scene};
for (const std::unique_ptr<Geometry> &geometry : all_geometries) {
if (geometry->geom_type() & GEOM_MESH) {
if (geometry->get_geom_type() == GEOM_MESH) {
MeshFromGeometry mesh_ob_from_geometry{bmain, *geometry, global_vertices, materials};
import_collection.add_object_to_collection(mesh_ob_from_geometry.mover());
}
else if (geometry->geom_type() & GEOM_CURVE) {
else if (geometry->get_geom_type() == GEOM_CURVE) {
CurveFromGeometry curve_ob_from_geometry(bmain, *geometry, global_vertices);
import_collection.add_object_to_collection(curve_ob_from_geometry.mover());
}