6
$\begingroup$

For a given plot (unknown Colorfunction) bild I would like to get the color of every single point {x,y,z}.

bild = ContourPlot3D[x^3 + y^2 - z^2, {x, -2, 2}, {y, -2, 2}, {z, -2, 2}, 
ColorFunction -> Function[{x, y, z}, Hue[x^2 + y^2 + z^2]], 
Mesh -> False]

enter image description here

I tried

pts=bild[[1, 1]][[1]]
Cases[ bild , VertexColors -> col_  :> col, -1] (* { } *)

which returns an empty set.

Any idea? Thanks!

$\endgroup$
2
  • $\begingroup$ It may help to try with bild = ContourPlot3D[x^3 + y^2 - z^2, {x, 0, 1}, {y, 0, 1}, {z, 0, 1}, Contours -> 1, ColorFunction -> Function[{x, y, z}, Hue[x^2 + y^2 + z^2]], Mesh -> False,PlotPoints->5] $\endgroup$
    – chris
    Dec 27, 2023 at 11:49
  • $\begingroup$ it seems then this works? tt = bild // FullForm // First // First // First; pts = tt[[1]]; col = tt[[3, 2]]; Transpose[{col, Map[Point, pts]}] // Graphics3D $\endgroup$
    – chris
    Dec 27, 2023 at 11:55

4 Answers 4

4
$\begingroup$

This also works but is probably less robust. It does not require redoing the plot though.

tt = bild // FullForm // First // First // First; 
pts = tt[[1]]; col = tt[[3, 2]]; 
Transpose[{col, Map[Point, pts]}] // Graphics3D 

enter image description here

$\endgroup$
3
  • $\begingroup$ Thanks for your answer and effort. VertexColor only gives a scalar as color information, I would have expected an RGB-triple Any idea how to interpret this scalar? $\endgroup$ Dec 27, 2023 at 12:50
  • $\begingroup$ I am not sure I understand: col[[1]] // Head returns Hue. $\endgroup$
    – chris
    Dec 27, 2023 at 12:55
  • $\begingroup$ Thanks, meanwhile I can reproduce your result! $\endgroup$ Dec 27, 2023 at 13:00
5
$\begingroup$

You may try with Sow and Reap.

{bild, pts} = 
  Reap[ContourPlot3D[
    x^3 + y^2 - z^2, {x, -2, 2}, {y, -2, 2}, {z, -2, 2}, 
    ColorFunction -> 
     Function[{x, y, z}, Sow[{Hue[x^2 + y^2 + z^2], {x, y, z}}]; 
      Hue[x^2 + y^2 + z^2]], Mesh -> False]];

Graphics3D[MapAt[Point, First@pts, {All, 2}]]

enter image description here

$\endgroup$
3
  • $\begingroup$ Thanks for this clear answer! $\endgroup$ Dec 27, 2023 at 13:05
  • $\begingroup$ It is astonishing that ContourPlot3D generates far fewer points here than in chris's answer $\endgroup$ Dec 27, 2023 at 13:37
  • $\begingroup$ @UlrichNeumann, I believe he set PlotPoints -> 50 (which I did not) – that's why he got more points :) $\endgroup$
    – Domen
    Dec 27, 2023 at 13:58
4
$\begingroup$
stylePointPairs = Cases[Normal[bild], 
   Polygon[coords_, VertexColors -> cols_, ___] :> 
    Thread[{cols, Point /@ coords}], All];

Graphics3D[{PointSize@Medium, stylePointPairs}]

enter image description here

You can also use:

stylePointPairs2 = Cases[bild, 
   GraphicsComplex[coords_, ___, VertexColors -> cols_, ___] :> 
    Thread[{cols, Point /@ coords}], All];

Graphics3D[{PointSize@Medium, stylePointPairs2}]

enter image description here

$\endgroup$
2
$\begingroup$
Normal[bild] /. 
  {_Line -> {}, 
  Polygon[coords_, vc : (VertexColors -> _), ___] :>
    {PointSize @ Medium, Point[coords, vc]}}

enter image description here

$\endgroup$
3
  • $\begingroup$ Thanks for your answer! Any idea why Cases[ bild , VertexColors -> col_ :> col, -1] doesn't work? $\endgroup$ Dec 27, 2023 at 13:59
  • $\begingroup$ @UlrichNeumann, we need to use Cases[bild, (VertexColors -> col_) :> col, -1] or Cases[bild, HoldPattern[(VertexColors -> col_)]:> col, -1] $\endgroup$
    – kglr
    Dec 27, 2023 at 14:04
  • 1
    $\begingroup$ Check VertexColors -> col_ :> col // FullForm to see how VertexColors -> col_ :> col is parsed. $\endgroup$
    – kglr
    Dec 27, 2023 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.