Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

r""" 

Texture Support 

 

This module provides texture/material support for 3D Graphics 

objects and plotting. This is a very rough common interface for 

Tachyon, x3d, and obj (mtl). See 

:meth:`Texture <sage.plot.plot3d.texture.Texture>` and 

:class:`Texture_class <sage.plot.plot3d.texture.Texture_class>` 

for full details about options and use. 

 

Initially, we have no textures set:: 

 

sage: sage.plot.plot3d.base.Graphics3d().texture_set() 

set() 

 

However, one can access these textures in the following manner:: 

 

sage: G = tetrahedron(color='red') + tetrahedron(color='yellow') + tetrahedron(color='red', opacity=0.5) 

sage: [t for t in G.texture_set() if t.color == colors.red] # we should have two red textures 

[Texture(texture..., red, ff0000), Texture(texture..., red, ff0000)] 

sage: [t for t in G.texture_set() if t.color == colors.yellow] # ...and one yellow 

[Texture(texture..., yellow, ffff00)] 

 

And the Texture objects keep track of all their data:: 

 

sage: T = tetrahedron(color='red', opacity=0.5) 

sage: t = T.get_texture() 

sage: t.opacity 

0.5 

sage: T # should be translucent 

Graphics3d Object 

 

AUTHOR: 

 

- Robert Bradshaw (2007-07-07) Initial version. 

 

""" 

from sage.misc.fast_methods import WithEqualityById 

from sage.structure.sage_object import SageObject 

 

from sage.plot.colors import Color 

 

 

uniq_c = 0 

def _new_global_texture_id(): 

""" 

Generate a new unique id for a texture. 

 

EXAMPLES:: 

 

sage: sage.plot.plot3d.texture._new_global_texture_id() 

'texture...' 

sage: sage.plot.plot3d.texture._new_global_texture_id() 

'texture...' 

""" 

global uniq_c 

uniq_c += 1 

return "texture%s" % uniq_c 

 

 

from sage.plot.colors import colors 

 

def is_Texture(x): 

r""" 

Return whether ``x`` is an instance of ``Texture_class``. 

 

EXAMPLES:: 

 

sage: from sage.plot.plot3d.texture import is_Texture, Texture 

sage: t = Texture(0.5) 

sage: is_Texture(t) 

True 

 

:: 

 

sage: is_Texture(4) 

False 

""" 

return isinstance(x, Texture_class) 

 

def Texture(id=None, **kwds): 

r""" 

Return a texture. 

 

INPUT: 

 

- ``id`` - a texture (optional, default: None), a dict, a color, a 

str, a tuple, None or any other type acting as an ID. If ``id`` is 

None and keyword ``texture`` is empty, then it returns a unique texture object. 

- ``texture`` - a texture 

- ``color`` - tuple or str, (optional, default: (.4, .4, 1)) 

- ``opacity`` - number between 0 and 1 (optional, default: 1) 

- ``ambient`` - number (optional, default: 0.5) 

- ``diffuse`` - number (optional, default: 1) 

- ``specular`` - number (optional, default: 0) 

- ``shininess`` - number (optional, default: 1) 

- ``name`` - str (optional, default: None) 

- ``**kwds`` - other valid keywords 

 

OUTPUT: 

 

A texture object. 

 

EXAMPLES: 

 

Texture from integer ``id``:: 

 

sage: from sage.plot.plot3d.texture import Texture 

sage: Texture(17) 

Texture(17, 6666ff) 

 

Texture from rational ``id``:: 

 

sage: Texture(3/4) 

Texture(3/4, 6666ff) 

 

Texture from a dict:: 

 

sage: Texture({'color':'orange','opacity':0.5}) 

Texture(texture..., orange, ffa500) 

 

Texture from a color:: 

 

sage: c = Color('red') 

sage: Texture(c) 

Texture(texture..., ff0000) 

 

Texture from a valid string color:: 

 

sage: Texture('red') 

Texture(texture..., red, ff0000) 

 

Texture from a non valid string color:: 

 

sage: Texture('redd') 

Texture(redd, 6666ff) 

 

Texture from a tuple:: 

 

sage: Texture((.2,.3,.4)) 

Texture(texture..., 334c66) 

 

Textures using other keywords:: 

 

sage: Texture(specular=0.4) 

Texture(texture..., 6666ff) 

sage: Texture(diffuse=0.4) 

Texture(texture..., 6666ff) 

sage: Texture(shininess=0.3) 

Texture(texture..., 6666ff) 

sage: Texture(ambient=0.7) 

Texture(texture..., 6666ff) 

""" 

if isinstance(id, Texture_class): 

return id 

if 'texture' in kwds: 

t = kwds['texture'] 

if is_Texture(t): 

return t 

else: 

raise TypeError("texture keyword must be a texture object") 

if isinstance(id, dict): 

kwds = id 

if 'rgbcolor' in kwds: 

kwds['color'] = kwds['rgbcolor'] 

id = None 

elif isinstance(id, Color): 

kwds['color'] = id.rgb() 

id = None 

elif isinstance(id, str) and id in colors: 

kwds['color'] = id 

id = None 

elif isinstance(id, tuple): 

kwds['color'] = id 

id = None 

if id is None: 

id = _new_global_texture_id() 

return Texture_class(id, **kwds) 

 

def parse_color(info, base=None): 

r""" 

Parses the color. 

 

It transforms a valid color string into a color object and 

a color object into an RBG tuple of length 3. Otherwise, 

it multiplies the info by the base color. 

 

INPUT: 

 

- ``info`` - color, valid color str or number 

- ``base`` - tuple of length 3 (optional, default: None) 

 

OUTPUT: 

 

A tuple or color. 

 

EXAMPLES: 

 

From a color:: 

 

sage: from sage.plot.plot3d.texture import parse_color 

sage: c = Color('red') 

sage: parse_color(c) 

(1.0, 0.0, 0.0) 

 

From a valid color str:: 

 

sage: parse_color('red') 

RGB color (1.0, 0.0, 0.0) 

sage: parse_color('#ff0000') 

RGB color (1.0, 0.0, 0.0) 

 

From a non valid color str:: 

 

sage: parse_color('redd') 

Traceback (most recent call last): 

... 

ValueError: unknown color 'redd' 

 

From an info and a base:: 

 

sage: opacity = 10 

sage: parse_color(opacity, base=(.2,.3,.4)) 

(2.0, 3.0, 4.0) 

""" 

if isinstance(info, Color): 

return info.rgb() 

elif isinstance(info, str): 

try: 

return Color(info) 

except KeyError: 

raise ValueError("unknown color '%s'"%info) 

else: 

r, g, b = base 

# We don't want to lose the data when we split it into its respective components. 

if not r: r = 1e-5 

if not g: g = 1e-5 

if not b: b = 1e-5 

return (float(info*r), float(info*g), float(info*b)) 

 

 

class Texture_class(WithEqualityById, SageObject): 

r""" 

Construction of a texture. 

 

See documentation of :meth:`Texture <sage.plot.plot3d.texture.Texture>` 

for more details and examples. 

 

EXAMPLES: 

 

We create a translucent texture:: 

 

sage: from sage.plot.plot3d.texture import Texture 

sage: t = Texture(opacity=0.6) 

sage: t 

Texture(texture..., 6666ff) 

sage: t.opacity 

0.6 

sage: t.jmol_str('obj') 

'color obj translucent 0.4 [102,102,255]' 

sage: t.mtl_str() 

'newmtl texture...\nKa 0.2 0.2 0.5\nKd 0.4 0.4 1.0\nKs 0.0 0.0 0.0\nillum 1\nNs 1.0\nd 0.6' 

sage: t.x3d_str() 

"<Appearance><Material diffuseColor='0.4 0.4 1.0' shininess='1.0' specularColor='0.0 0.0 0.0'/></Appearance>" 

 

TESTS:: 

 

sage: Texture(opacity=1/3).opacity 

0.3333333333333333 

 

sage: hash(Texture()) # random 

42 

""" 

def __init__(self, id, color=(.4, .4, 1), opacity=1, ambient=0.5, diffuse=1, specular=0, shininess=1, name=None, **kwds): 

r""" 

Construction of a texture. 

 

See documentation of :meth:`Texture <sage.plot.plot3d.texture.Texture>` 

for more details and examples. 

 

EXAMPLES:: 

 

sage: from sage.plot.plot3d.texture import Texture_class 

sage: Texture_class(3, opacity=0.6) 

Texture(3, 6666ff) 

""" 

self.id = id 

if name is None and isinstance(color, str): 

name = color 

self.name = name 

 

if not isinstance(color, tuple): 

color = parse_color(color) 

else: 

if len(color) == 4: 

opacity = color[3] 

color = (float(color[0]), float(color[1]), float(color[2])) 

 

self.color = color 

self.opacity = float(opacity) 

self.shininess = float(shininess) 

 

if not isinstance(ambient, tuple): 

ambient = parse_color(ambient, color) 

self.ambient = ambient 

 

if not isinstance(diffuse, tuple): 

diffuse = parse_color(diffuse, color) 

self.diffuse = diffuse 

 

if not isinstance(specular, tuple): 

specular = parse_color(specular, color) 

self.specular = specular 

 

def _repr_(self): 

""" 

Gives string representation of the Texture object. 

 

EXAMPLES:: 

 

sage: from sage.plot.plot3d.texture import Texture 

sage: Texture('yellow') #indirect doctest 

Texture(texture..., yellow, ffff00) 

sage: Texture((1,1,0), opacity=.5) 

Texture(texture..., ffff00) 

""" 

if self.name is not None: 

return "Texture(%s, %s, %s)" % (self.id, self.name, self.hex_rgb()) 

else: 

return "Texture(%s, %s)" % (self.id, self.hex_rgb()) 

 

def hex_rgb(self): 

""" 

EXAMPLES:: 

 

sage: from sage.plot.plot3d.texture import Texture 

sage: Texture('red').hex_rgb() 

'ff0000' 

sage: Texture((1, .5, 0)).hex_rgb() 

'ff7f00' 

""" 

return "%02x%02x%02x" % tuple(int(255*s) for s in self.color) 

 

def tachyon_str(self): 

r""" 

Converts Texture object to string suitable for Tachyon ray tracer. 

 

EXAMPLES:: 

 

sage: from sage.plot.plot3d.texture import Texture 

sage: t = Texture(opacity=0.6) 

sage: t.tachyon_str() 

'Texdef texture...\n Ambient 0.333333333333 Diffuse 0.666666666667 Specular 0.0 Opacity 0.6\n Color 0.4 0.4 1.0\n TexFunc 0' 

""" 

total_color = float(sum(self.ambient) + sum(self.diffuse) + sum(self.specular)) 

if total_color == 0: 

total_color = 1 

return "Texdef %s\n" % self.id + \ 

" Ambient %s Diffuse %s Specular %s Opacity %s\n" % \ 

(sum(self.ambient)/total_color, 

sum(self.diffuse)/total_color, 

sum(self.specular)/total_color, 

self.opacity) + \ 

" Color %s %s %s\n" % (self.color[0], self.color[1], self.color[2]) + \ 

" TexFunc 0" 

 

def x3d_str(self): 

r""" 

Converts Texture object to string suitable for x3d. 

 

EXAMPLES:: 

 

sage: from sage.plot.plot3d.texture import Texture 

sage: t = Texture(opacity=0.6) 

sage: t.x3d_str() 

"<Appearance><Material diffuseColor='0.4 0.4 1.0' shininess='1.0' specularColor='0.0 0.0 0.0'/></Appearance>" 

""" 

return "<Appearance><Material diffuseColor='%s %s %s' shininess='%s' specularColor='%s %s %s'/></Appearance>" % \ 

(self.color[0], self.color[1], self.color[2], self.shininess, self.specular[0], self.specular[0], self.specular[0]) 

 

def mtl_str(self): 

r""" 

Converts Texture object to string suitable for mtl output. 

 

EXAMPLES:: 

 

sage: from sage.plot.plot3d.texture import Texture 

sage: t = Texture(opacity=0.6) 

sage: t.mtl_str() 

'newmtl texture...\nKa 0.2 0.2 0.5\nKd 0.4 0.4 1.0\nKs 0.0 0.0 0.0\nillum 1\nNs 1.0\nd 0.6' 

""" 

return "\n".join(["newmtl %s" % self.id, 

"Ka %s %s %s" % self.ambient, 

"Kd %s %s %s" % self.diffuse, 

"Ks %s %s %s" % self.specular, 

"illum %s" % (2 if sum(self.specular) > 0 else 1), 

"Ns %s" % self.shininess, 

"d %s" % self.opacity, ]) 

 

def jmol_str(self, obj): 

r""" 

Converts Texture object to string suitable for Jmol applet. 

 

INPUT: 

 

- ``obj`` - str 

 

EXAMPLES:: 

 

sage: from sage.plot.plot3d.texture import Texture 

sage: t = Texture(opacity=0.6) 

sage: t.jmol_str('obj') 

'color obj translucent 0.4 [102,102,255]' 

 

:: 

 

sage: sum([dodecahedron(center=[2.5*x, 0, 0], color=(1, 0, 0, x/10)) for x in range(11)]).show(aspect_ratio=[1,1,1], frame=False, zoom=2) 

""" 

translucent = "translucent %s" % float(1-self.opacity) if self.opacity < 1 else "" 

return "color %s %s [%s,%s,%s]" % (obj, translucent, 

int(255*self.color[0]), int(255*self.color[1]), int(255*self.color[2]))