|
| 1 | +////////////////////////////////////////////////////////////////////////// |
| 2 | +// |
| 3 | +// Copyright (c) 2007-2013, Image Engine Design Inc. All rights reserved. |
| 4 | +// |
| 5 | +// Redistribution and use in source and binary forms, with or without |
| 6 | +// modification, are permitted provided that the following conditions are |
| 7 | +// met: |
| 8 | +// |
| 9 | +// * Redistributions of source code must retain the above copyright |
| 10 | +// notice, this list of conditions and the following disclaimer. |
| 11 | +// |
| 12 | +// * Redistributions in binary form must reproduce the above copyright |
| 13 | +// notice, this list of conditions and the following disclaimer in the |
| 14 | +// documentation and/or other materials provided with the distribution. |
| 15 | +// |
| 16 | +// * Neither the name of Image Engine Design nor the names of any |
| 17 | +// other contributors to this software may be used to endorse or |
| 18 | +// promote products derived from this software without specific prior |
| 19 | +// written permission. |
| 20 | +// |
| 21 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 22 | +// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 23 | +// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 24 | +// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 25 | +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 26 | +// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 27 | +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 | +// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 | +// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 | +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 | +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | +// |
| 33 | +////////////////////////////////////////////////////////////////////////// |
| 34 | + |
| 35 | +#include <cassert> |
| 36 | + |
| 37 | +#include "boost/format.hpp" |
| 38 | + |
| 39 | +#include "maya/MFnNurbsCurve.h" |
| 40 | +#include "maya/MPointArray.h" |
| 41 | +#include "maya/MDoubleArray.h" |
| 42 | + |
| 43 | +#include "IECore/CurvesPrimitive.h" |
| 44 | +#include "IECore/PrimitiveVariable.h" |
| 45 | +#include "IECore/MessageHandler.h" |
| 46 | +#include "IECore/CompoundParameter.h" |
| 47 | + |
| 48 | +#include "IECoreMaya/Convert.h" |
| 49 | +#include "IECoreMaya/ToMayaCurveConverter.h" |
| 50 | + |
| 51 | +using namespace IECoreMaya; |
| 52 | + |
| 53 | +ToMayaCurveConverter::Description ToMayaCurveConverter::g_curvesDataDescription( IECore::CurvesPrimitive::staticTypeId(), MFn::kNurbsCurveData ); |
| 54 | +ToMayaCurveConverter::Description ToMayaCurveConverter::g_curvesDescription( IECore::CurvesPrimitive::staticTypeId(), MFn::kNurbsCurve ); |
| 55 | + |
| 56 | +ToMayaCurveConverter::ToMayaCurveConverter( IECore::ConstObjectPtr object ) |
| 57 | +: ToMayaObjectConverter( "Converts IECore::CurvesPrimitive objects to a Maya object.", object) |
| 58 | +{ |
| 59 | + m_indexParameter = new IECore::IntParameter( "index", "The index of the curve to be converted.", 0 ); |
| 60 | + parameters()->addParameter( m_indexParameter ); |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +IECore::IntParameterPtr ToMayaCurveConverter::indexParameter() |
| 65 | +{ |
| 66 | + return m_indexParameter; |
| 67 | +} |
| 68 | + |
| 69 | +IECore::ConstIntParameterPtr ToMayaCurveConverter::indexParameter() const |
| 70 | +{ |
| 71 | + return m_indexParameter; |
| 72 | +} |
| 73 | + |
| 74 | +bool ToMayaCurveConverter::doConversion( IECore::ConstObjectPtr from, MObject &to, IECore::ConstCompoundObjectPtr operands ) const |
| 75 | +{ |
| 76 | + MStatus s; |
| 77 | + |
| 78 | + IECore::ConstCurvesPrimitivePtr curves = IECore::runTimeCast<const IECore::CurvesPrimitive>( from ); |
| 79 | + |
| 80 | + assert( curves ); |
| 81 | + |
| 82 | + if ( !curves->arePrimitiveVariablesValid() || !curves->numCurves() ) |
| 83 | + { |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + int curveIndex = indexParameter()->getNumericValue(); |
| 88 | + if( curveIndex < 0 || curveIndex >= (int)curves->numCurves() ) |
| 89 | + { |
| 90 | + IECore::msg( IECore::Msg::Warning,"ToMayaCurveConverter::doConversion", boost::format( "Invalid curve index \"%d\"") % curveIndex ); |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + IECore::ConstV3fVectorDataPtr p = curves->variableData< IECore::V3fVectorData >( "P", IECore::PrimitiveVariable::Vertex ); |
| 95 | + if( !p ) |
| 96 | + { |
| 97 | + IECore::msg( IECore::Msg::Warning,"ToMayaCurveConverter::doConversion", "Curve has no \"P\" data" ); |
| 98 | + return false; |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + const std::vector<int>& verticesPerCurve = curves->verticesPerCurve()->readable(); |
| 103 | + int curveBase = 0; |
| 104 | + for( int i=0; i<curveIndex; ++i ) |
| 105 | + { |
| 106 | + curveBase += verticesPerCurve[i]; |
| 107 | + } |
| 108 | + |
| 109 | + MPointArray vertexArray; |
| 110 | + |
| 111 | + int numVertices = verticesPerCurve[curveIndex]; |
| 112 | + |
| 113 | + const std::vector<Imath::V3f>& pts = p->readable(); |
| 114 | + |
| 115 | + // triple up the start points for cubic periodic curves: |
| 116 | + if( curves->periodic() && curves->basis() != IECore::CubicBasisf::linear() ) |
| 117 | + { |
| 118 | + vertexArray.append( IECore::convert<MPoint, Imath::V3f>( pts[curveBase] ) ); |
| 119 | + vertexArray.append( vertexArray[0] ); |
| 120 | + } |
| 121 | + |
| 122 | + for( int i = 0; i < numVertices; ++i ) |
| 123 | + { |
| 124 | + vertexArray.append( IECore::convert<MPoint, Imath::V3f>( pts[i + curveBase] ) ); |
| 125 | + } |
| 126 | + |
| 127 | + // if the curve is periodic, the first N cvs must be identical to the last N cvs, where N is the degree |
| 128 | + // of the curve: |
| 129 | + if( curves->periodic() ) |
| 130 | + { |
| 131 | + if( curves->basis() == IECore::CubicBasisf::linear() ) |
| 132 | + { |
| 133 | + // linear: N = 1 |
| 134 | + vertexArray.append( vertexArray[0] ); |
| 135 | + } |
| 136 | + else |
| 137 | + { |
| 138 | + // cubic: N = 3 |
| 139 | + vertexArray.append( vertexArray[0] ); |
| 140 | + vertexArray.append( vertexArray[1] ); |
| 141 | + vertexArray.append( vertexArray[2] ); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + MDoubleArray knotSequences; |
| 146 | + if( curves->basis() == IECore::CubicBasisf::linear() ) |
| 147 | + { |
| 148 | + for( unsigned i=0; i < vertexArray.length(); ++i ) |
| 149 | + { |
| 150 | + knotSequences.append( i ); |
| 151 | + } |
| 152 | + } |
| 153 | + else |
| 154 | + { |
| 155 | + // first two cvs must have coincident knots if the curve's open, otherwise |
| 156 | + // they must be spaced out |
| 157 | + knotSequences.append( curves->periodic() ? -1 : 0 ); |
| 158 | + for( unsigned i=0; i < vertexArray.length(); ++i ) |
| 159 | + { |
| 160 | + knotSequences.append( i ); |
| 161 | + } |
| 162 | + // same with the last two: |
| 163 | + knotSequences.append( curves->periodic() ? vertexArray.length() : vertexArray.length() - 1 ); |
| 164 | + } |
| 165 | + |
| 166 | + MFnNurbsCurve fnCurve; |
| 167 | + fnCurve.create( vertexArray, knotSequences, curves->basis() == IECore::CubicBasisf::linear() ? 1 : 3, curves->periodic() ? MFnNurbsCurve::kPeriodic : MFnNurbsCurve::kOpen, false, false, to, &s ); |
| 168 | + if (!s) |
| 169 | + { |
| 170 | + IECore::msg( IECore::Msg::Warning,"ToMayaCurveConverter::doConversion", s.errorString().asChar() ); |
| 171 | + return false; |
| 172 | + } |
| 173 | + |
| 174 | + return true; |
| 175 | +} |
0 commit comments