Welcome to mirror list, hosted at ThFree Co, Russian Federation.

abc_writer_curves.cc « exporter « alembic « io « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5e01609f930f3db2292c802aebbc2f9e96334329 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2016 Kévin Dietrich.
 * All rights reserved.
 */

/** \file
 * \ingroup balembic
 */

#include "abc_writer_curves.h"
#include "intern/abc_axis_conversion.h"

#include "DNA_curve_types.h"
#include "DNA_object_types.h"

#include "BKE_curve.h"
#include "BKE_mesh.h"
#include "BKE_object.h"

#include "CLG_log.h"
static CLG_LogRef LOG = {"io.alembic"};

using Alembic::AbcGeom::OCompoundProperty;
using Alembic::AbcGeom::OCurves;
using Alembic::AbcGeom::OCurvesSchema;
using Alembic::AbcGeom::OInt16Property;
using Alembic::AbcGeom::ON3fGeomParam;
using Alembic::AbcGeom::OV2fGeomParam;

namespace blender::io::alembic {

const std::string ABC_CURVE_RESOLUTION_U_PROPNAME("blender:resolution");

ABCCurveWriter::ABCCurveWriter(const ABCWriterConstructorArgs &args) : ABCAbstractWriter(args)
{
}

void ABCCurveWriter::create_alembic_objects(const HierarchyContext *context)
{
  CLOG_INFO(&LOG, 2, "exporting %s", args_.abc_path.c_str());
  abc_curve_ = OCurves(args_.abc_parent, args_.abc_name, timesample_index_);
  abc_curve_schema_ = abc_curve_.getSchema();

  Curve *cu = static_cast<Curve *>(context->object->data);
  OCompoundProperty user_props = abc_curve_schema_.getUserProperties();
  OInt16Property user_prop_resolu(user_props, ABC_CURVE_RESOLUTION_U_PROPNAME);
  user_prop_resolu.set(cu->resolu);
}

Alembic::Abc::OObject ABCCurveWriter::get_alembic_object() const
{
  return abc_curve_;
}

Alembic::Abc::OCompoundProperty ABCCurveWriter::abc_prop_for_custom_props()
{
  return abc_schema_prop_for_custom_props(abc_curve_schema_);
}

void ABCCurveWriter::do_write(HierarchyContext &context)
{
  Curve *curve = static_cast<Curve *>(context.object->data);

  std::vector<Imath::V3f> verts;
  std::vector<int32_t> vert_counts;
  std::vector<float> widths;
  std::vector<float> weights;
  std::vector<float> knots;
  std::vector<uint8_t> orders;
  Imath::V3f temp_vert;

  Alembic::AbcGeom::BasisType curve_basis = Alembic::AbcGeom::kNoBasis;
  Alembic::AbcGeom::CurveType curve_type = Alembic::AbcGeom::kVariableOrder;
  Alembic::AbcGeom::CurvePeriodicity periodicity = Alembic::AbcGeom::kNonPeriodic;

  Nurb *nurbs = static_cast<Nurb *>(curve->nurb.first);
  for (; nurbs; nurbs = nurbs->next) {
    if (nurbs->bp) {
      curve_basis = Alembic::AbcGeom::kNoBasis;
      curve_type = Alembic::AbcGeom::kVariableOrder;

      const int totpoint = nurbs->pntsu * nurbs->pntsv;

      const BPoint *point = nurbs->bp;

      for (int i = 0; i < totpoint; i++, point++) {
        copy_yup_from_zup(temp_vert.getValue(), point->vec);
        verts.push_back(temp_vert);
        weights.push_back(point->vec[3]);
        widths.push_back(point->radius);
      }
    }
    else if (nurbs->bezt) {
      curve_basis = Alembic::AbcGeom::kBezierBasis;
      curve_type = Alembic::AbcGeom::kCubic;

      const int totpoint = nurbs->pntsu;

      const BezTriple *bezier = nurbs->bezt;

      /* TODO(kevin): store info about handles, Alembic doesn't have this. */
      for (int i = 0; i < totpoint; i++, bezier++) {
        copy_yup_from_zup(temp_vert.getValue(), bezier->vec[1]);
        verts.push_back(temp_vert);
        widths.push_back(bezier->radius);
      }
    }

    if ((nurbs->flagu & CU_NURB_ENDPOINT) != 0) {
      periodicity = Alembic::AbcGeom::kNonPeriodic;
    }
    else if ((nurbs->flagu & CU_NURB_CYCLIC) != 0) {
      periodicity = Alembic::AbcGeom::kPeriodic;

      /* Duplicate the start points to indicate that the curve is actually
       * cyclic since other software need those.
       */

      for (int i = 0; i < nurbs->orderu; i++) {
        verts.push_back(verts[i]);
      }
    }

    if (nurbs->knotsu != nullptr) {
      const size_t num_knots = KNOTSU(nurbs);

      /* Add an extra knot at the beginning and end of the array since most apps
       * require/expect them. */
      knots.resize(num_knots + 2);

      for (int i = 0; i < num_knots; i++) {
        knots[i + 1] = nurbs->knotsu[i];
      }

      if ((nurbs->flagu & CU_NURB_CYCLIC) != 0) {
        knots[0] = nurbs->knotsu[0];
        knots[num_knots - 1] = nurbs->knotsu[num_knots - 1];
      }
      else {
        knots[0] = (2.0f * nurbs->knotsu[0] - nurbs->knotsu[1]);
        knots[num_knots - 1] = (2.0f * nurbs->knotsu[num_knots - 1] -
                                nurbs->knotsu[num_knots - 2]);
      }
    }

    orders.push_back(nurbs->orderu);
    vert_counts.push_back(verts.size());
  }

  Alembic::AbcGeom::OFloatGeomParam::Sample width_sample;
  width_sample.setVals(widths);

  OCurvesSchema::Sample sample(verts,
                               vert_counts,
                               curve_type,
                               periodicity,
                               width_sample,
                               OV2fGeomParam::Sample(), /* UVs */
                               ON3fGeomParam::Sample(), /* normals */
                               curve_basis,
                               weights,
                               orders,
                               knots);

  update_bounding_box(context.object);
  sample.setSelfBounds(bounding_box_);
  abc_curve_schema_.set(sample);
}

ABCCurveMeshWriter::ABCCurveMeshWriter(const ABCWriterConstructorArgs &args)
    : ABCGenericMeshWriter(args)
{
}

Mesh *ABCCurveMeshWriter::get_export_mesh(Object *object_eval, bool &r_needsfree)
{
  Mesh *mesh_eval = BKE_object_get_evaluated_mesh(object_eval);
  if (mesh_eval != nullptr) {
    /* Mesh_eval only exists when generative modifiers are in use. */
    r_needsfree = false;
    return mesh_eval;
  }

  r_needsfree = true;
  return BKE_mesh_new_nomain_from_curve(object_eval);
}

}  // namespace blender::io::alembic