-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from qmuntal/decode-exts
Decode Extensions
- Loading branch information
Showing
44 changed files
with
2,221 additions
and
1,715 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package beamlattice | ||
|
||
import ( | ||
"encoding/xml" | ||
"strconv" | ||
|
||
"github.com/qmuntal/go3mf" | ||
) | ||
|
||
// Marshal3MF encodes the resource. | ||
func (m *BeamLattice) Marshal3MF(x *go3mf.XMLEncoder) error { | ||
xs := xml.StartElement{Name: xml.Name{Space: ExtensionName, Local: attrBeamLattice}, Attr: []xml.Attr{ | ||
{Name: xml.Name{Local: attrMinLength}, Value: strconv.FormatFloat(float64(m.MinLength), 'f', x.FloatPresicion(), 32)}, | ||
{Name: xml.Name{Local: attrRadius}, Value: strconv.FormatFloat(float64(m.DefaultRadius), 'f', x.FloatPresicion(), 32)}, | ||
}} | ||
if m.ClipMode != ClipNone { | ||
xs.Attr = append(xs.Attr, xml.Attr{Name: xml.Name{Local: attrClippingMode}, Value: m.ClipMode.String()}) | ||
} | ||
if m.ClippingMeshID != 0 { | ||
xs.Attr = append(xs.Attr, xml.Attr{ | ||
Name: xml.Name{Local: attrClippingMesh}, | ||
Value: strconv.FormatUint(uint64(m.ClippingMeshID), 10)}, | ||
) | ||
} | ||
if m.RepresentationMeshID != 0 { | ||
xs.Attr = append(xs.Attr, xml.Attr{ | ||
Name: xml.Name{Local: attrRepresentationMesh}, | ||
Value: strconv.FormatUint(uint64(m.RepresentationMeshID), 10)}, | ||
) | ||
} | ||
if m.CapMode != CapModeSphere { | ||
xs.Attr = append(xs.Attr, xml.Attr{Name: xml.Name{Local: attrCap}, Value: m.CapMode.String()}) | ||
} | ||
x.EncodeToken(xs) | ||
|
||
marshalBeams(x, m) | ||
marshalBeamsets(x, m) | ||
|
||
x.EncodeToken(xs.End()) | ||
return nil | ||
} | ||
|
||
func marshalBeamsets(x *go3mf.XMLEncoder, m *BeamLattice) { | ||
xb := xml.StartElement{Name: xml.Name{Space: ExtensionName, Local: attrBeamSets}} | ||
x.EncodeToken(xb) | ||
for _, bs := range m.BeamSets { | ||
xbs := xml.StartElement{Name: xml.Name{Space: ExtensionName, Local: attrBeamSet}} | ||
if bs.Name != "" { | ||
xbs.Attr = append(xbs.Attr, xml.Attr{Name: xml.Name{Local: attrName}, Value: bs.Name}) | ||
} | ||
if bs.Identifier != "" { | ||
xbs.Attr = append(xbs.Attr, xml.Attr{Name: xml.Name{Local: attrIdentifier}, Value: bs.Identifier}) | ||
} | ||
x.EncodeToken(xbs) | ||
x.SetAutoClose(true) | ||
for _, ref := range bs.Refs { | ||
x.EncodeToken(xml.StartElement{Name: xml.Name{Space: ExtensionName, Local: attrRef}, Attr: []xml.Attr{ | ||
{Name: xml.Name{Local: attrIndex}, Value: strconv.FormatUint(uint64(ref), 10)}, | ||
}}) | ||
} | ||
x.SetAutoClose(false) | ||
x.EncodeToken(xbs.End()) | ||
} | ||
x.EncodeToken(xb.End()) | ||
} | ||
|
||
func marshalBeams(x *go3mf.XMLEncoder, m *BeamLattice) { | ||
xb := xml.StartElement{Name: xml.Name{Space: ExtensionName, Local: attrBeams}} | ||
x.EncodeToken(xb) | ||
x.SetAutoClose(true) | ||
for _, b := range m.Beams { | ||
xbeam := xml.StartElement{Name: xml.Name{Space: ExtensionName, Local: attrBeam}, Attr: []xml.Attr{ | ||
{Name: xml.Name{Local: attrV1}, Value: strconv.FormatUint(uint64(b.NodeIndices[0]), 10)}, | ||
{Name: xml.Name{Local: attrV2}, Value: strconv.FormatUint(uint64(b.NodeIndices[1]), 10)}, | ||
}} | ||
if b.Radius[0] > 0 && b.Radius[0] != m.DefaultRadius { | ||
xbeam.Attr = append(xbeam.Attr, xml.Attr{ | ||
Name: xml.Name{Local: attrR1}, | ||
Value: strconv.FormatFloat(float64(b.Radius[0]), 'f', x.FloatPresicion(), 32), | ||
}) | ||
} | ||
if b.Radius[1] > 0 && b.Radius[1] != m.DefaultRadius { | ||
xbeam.Attr = append(xbeam.Attr, xml.Attr{ | ||
Name: xml.Name{Local: attrR2}, | ||
Value: strconv.FormatFloat(float64(b.Radius[1]), 'f', x.FloatPresicion(), 32), | ||
}) | ||
} | ||
if b.CapMode[0] != m.CapMode { | ||
xbeam.Attr = append(xbeam.Attr, xml.Attr{Name: xml.Name{Local: attrCap1}, Value: b.CapMode[0].String()}) | ||
} | ||
if b.CapMode[1] != m.CapMode { | ||
xbeam.Attr = append(xbeam.Attr, xml.Attr{Name: xml.Name{Local: attrCap2}, Value: b.CapMode[1].String()}) | ||
} | ||
x.EncodeToken(xbeam) | ||
} | ||
x.SetAutoClose(false) | ||
x.EncodeToken(xb.End()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package beamlattice | ||
|
||
import ( | ||
"encoding/xml" | ||
"testing" | ||
|
||
"github.com/go-test/deep" | ||
"github.com/qmuntal/go3mf" | ||
) | ||
|
||
func TestMarshalModel(t *testing.T) { | ||
meshLattice := &go3mf.Object{ | ||
ID: 15, Name: "Box", | ||
Mesh: &go3mf.Mesh{ | ||
Faces: []go3mf.Face{}, | ||
Extension: go3mf.Extension{ | ||
ExtensionName: &BeamLattice{ClipMode: ClipInside, ClippingMeshID: 8, RepresentationMeshID: 8}, | ||
}}, | ||
} | ||
beamLattice := MeshBeamLattice(meshLattice.Mesh) | ||
beamLattice.MinLength = 0.0001 | ||
beamLattice.CapMode = CapModeHemisphere | ||
beamLattice.DefaultRadius = 1 | ||
meshLattice.Mesh.Nodes = append(meshLattice.Mesh.Nodes, []go3mf.Point3D{ | ||
{45, 55, 55}, | ||
{45, 45, 55}, | ||
{45, 55, 45}, | ||
{45, 45, 45}, | ||
{55, 55, 45}, | ||
{55, 55, 55}, | ||
{55, 45, 55}, | ||
{55, 45, 45}, | ||
}...) | ||
beamLattice.BeamSets = append(beamLattice.BeamSets, BeamSet{Name: "test", Identifier: "set_id", Refs: []uint32{1}}) | ||
beamLattice.Beams = append(beamLattice.Beams, []Beam{ | ||
{NodeIndices: [2]uint32{0, 1}, Radius: [2]float32{1.5, 1.6}, CapMode: [2]CapMode{CapModeSphere, CapModeButt}}, | ||
{NodeIndices: [2]uint32{2, 0}, Radius: [2]float32{3, 1.5}, CapMode: [2]CapMode{CapModeSphere, CapModeHemisphere}}, | ||
{NodeIndices: [2]uint32{1, 3}, Radius: [2]float32{1.6, 3}, CapMode: [2]CapMode{CapModeHemisphere, CapModeHemisphere}}, | ||
{NodeIndices: [2]uint32{3, 2}, Radius: [2]float32{1, 1}, CapMode: [2]CapMode{CapModeHemisphere, CapModeHemisphere}}, | ||
{NodeIndices: [2]uint32{2, 4}, Radius: [2]float32{3, 2}, CapMode: [2]CapMode{CapModeHemisphere, CapModeHemisphere}}, | ||
{NodeIndices: [2]uint32{4, 5}, Radius: [2]float32{2, 2}, CapMode: [2]CapMode{CapModeHemisphere, CapModeHemisphere}}, | ||
{NodeIndices: [2]uint32{5, 6}, Radius: [2]float32{2, 2}, CapMode: [2]CapMode{CapModeHemisphere, CapModeHemisphere}}, | ||
{NodeIndices: [2]uint32{7, 6}, Radius: [2]float32{2, 2}, CapMode: [2]CapMode{CapModeHemisphere, CapModeHemisphere}}, | ||
{NodeIndices: [2]uint32{1, 6}, Radius: [2]float32{1.6, 2}, CapMode: [2]CapMode{CapModeHemisphere, CapModeHemisphere}}, | ||
{NodeIndices: [2]uint32{7, 4}, Radius: [2]float32{2, 2}, CapMode: [2]CapMode{CapModeHemisphere, CapModeHemisphere}}, | ||
{NodeIndices: [2]uint32{7, 3}, Radius: [2]float32{2, 3}, CapMode: [2]CapMode{CapModeHemisphere, CapModeHemisphere}}, | ||
{NodeIndices: [2]uint32{0, 5}, Radius: [2]float32{1.5, 2}, CapMode: [2]CapMode{CapModeHemisphere, CapModeButt}}, | ||
}...) | ||
|
||
m := &go3mf.Model{Path: "/3D/3dmodel.model", Namespaces: []xml.Name{{Space: ExtensionName, Local: "b"}}, Resources: go3mf.Resources{ | ||
Objects: []*go3mf.Object{meshLattice}, | ||
}} | ||
|
||
t.Run("base", func(t *testing.T) { | ||
b, err := go3mf.MarshalModel(m) | ||
if err != nil { | ||
t.Errorf("beamlattice.MarshalModel() error = %v", err) | ||
return | ||
} | ||
d := go3mf.NewDecoder(nil, 0) | ||
RegisterExtension(d) | ||
newModel := new(go3mf.Model) | ||
newModel.Path = m.Path | ||
if err := d.UnmarshalModel(b, newModel); err != nil { | ||
t.Errorf("beamlattice.MarshalModel() error decoding = %v, s = %s", err, string(b)) | ||
return | ||
} | ||
if diff := deep.Equal(m, newModel); diff != nil { | ||
t.Errorf("beamlattice.MarshalModel() = %v, s = %s", diff, string(b)) | ||
} | ||
}) | ||
} |
Oops, something went wrong.