Initialize a triangle mesh

์กฐํšŒ์ˆ˜ 18ํšŒ
์ฝ์ง€ ์•Š์€ ์ฒซ ๋ฉ”์‹œ์ง€๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

Maia R.

์ฝ์ง€ ์•Š์Œ,
2021. 1. 12. ์˜คํ›„ 2:47:4521. 1. 12.
๋ฐ›๋Š”์‚ฌ๋žŒ scalismo
Hi,
I have a question, both for Scala and Scalismo.

I have created a case class that needs a field of TriangleMesh type. As far as I know, class fields need to be initialized in Scala. How would you initialize a mesh as below ?
======
case class myClass(data: String) {

var referenceMesh: TriangleMesh[_3D] = ???

def setReferenceMesh(mesh: TriangleMesh[_3D]): Unit =ย  {
referenceMeshย  = mesh
}

// other methods which use referenecMesh
def method1(): Unit = {
// use referenceMesh field hereย  ย 

}

Thank you
Best regards

Marcel Luethi

์ฝ์ง€ ์•Š์Œ,
2021. 1. 13. ์˜ค์ „ 6:02:1621. 1. 13.
๋ฐ›๋Š”์‚ฌ๋žŒ Maia R., scalismo
Hi Maia

The ideal solution would be to pass the reference mesh in the constructor. This has the advantage, that the variable is never uninitialized and the object is always in a valid state. This would allow you do use a val instead of a var, which is preferred in Scala.

While it is possible to use setter methods in Scala, it is usually discouraged.ย  If you really have to use this approach, you can either initialize it with null or, preferably, use instead a variable of type Option[Triangle], which you initially set to None and then reset it to Some(mesh). This will avoid these nasty nullpointer exceptions and safe you a lot of trouble later. But as said, the best option is not to use var in the first place.

Best regards,

Marcel

--
You received this message because you are subscribed to the Google Groups "scalismo" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalismo+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scalismo/4af297d7-0606-4068-9a18-4f15d28026f9n%40googlegroups.com.

Andreas Morel-Forster

์ฝ์ง€ ์•Š์Œ,
2021. 1. 13. ์˜ค์ „ 6:09:1021. 1. 13.
๋ฐ›๋Š”์‚ฌ๋žŒ scal...@googlegroups.com

A bit out of sync as I accidently replied privately earlier on instead on this list :-/

Hi,

Without knowing your application you havI think it would be better Scala if you would use a case class including the referenceMesh as a field as data:

case class MyClass(data: String, reference: TriangleMesh[_3D])

val instance: MyClass = ???
val newInstance = instance.copy(reference = newReference)


To create a dummy mesh without anything you can use the following code:

var mesh = TriangleMesh3D(IndexedSeq[Point3D](), TriangleList(IndexedSeq[TriangleCell]()))


Best, Andreas

V R

์ฝ์ง€ ์•Š์Œ,
2021. 1. 13. ์˜ค์ „ 8:05:4421. 1. 13.
๋ฐ›๋Š”์‚ฌ๋žŒ Andreas Morel-Forster, Marcel Luethi, scalismo
Hi,ย 
Thank you very much for all. I includeย  referenceMesh in my case class as Andreas has suggested and everything is working very well.
Best regards,
Maia

V R

์ฝ์ง€ ์•Š์Œ,
2021. 1. 13. ์˜ค์ „ 8:11:4521. 1. 13.
๋ฐ›๋Š”์‚ฌ๋žŒ Marcel Luethi, scalismo
Hi Marcel,
Thank you very much for the suggestion and explanation. I followed your advice and everything is working very well (sorry I didn'tย mentionย your solution earlier, I saw your messageย after Andreas's one).
Best regards,
Maia
์ „์ฒด๋‹ต์žฅ
์ž‘์„ฑ์ž์—๊ฒŒ ๋‹ต๊ธ€
์ „๋‹ฌ
์ƒˆ ๋ฉ”์‹œ์ง€ 0๊ฐœ